2011-12-13 50 views
0

所以我有一些问题。问题的目标是,当我尝试从用户控件和Ajax调用Web服务时,我得到了500个内部服务器错误。ASP.NET - 从用户控件(.Ascx)调用Web服务返回错误500内部服务器错误

还有就是我的代码示例:

Web服务.CS

using System; 
using System.Collections.Generic; 
using System.Web; 
using System.Web.Services; 


[WebService(Namespace = "http://tempuri.org/")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 

public class BudgetJson : System.Web.Services.WebService { 

    public BudgetJson() 
    { 
    } 



    [WebMethod] 
    public static String GetRecordJson() 
    { 
     return " Hello Master, I'm Json Data "; 
    } 
} 

用户控件(.ascx)文件(AJAX调用)

$(document).ready(function() { 
      $.ajax({ 
       type: "POST", 
       url: "BudgetJson.asmx", 
       data: "{}", 
       contentType: "application/json; charset=utf-8", 
       dataType: "json", 
       success: function (msg) 
       { 
        alert(msg); 
       } 
      }); 
     }); 

所以,当页面加载和要求我得到了这样的回应:

soap:ReceiverSystem.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.Xml.XmlException: Data at the root level is invalid. Line 1, position 1. at System.Xml.XmlTextReaderImpl.Throw(Exception e) at System.Xml.XmlTextReaderImpl.Throw(String res, String arg) at System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace() at System.Xml.XmlTextReaderImpl.ParseDocumentContent() at System.Xml.XmlTextReaderImpl.Read() at System.Xml.XmlTextReader.Read() at System.Web.Services.Protocols.SoapServerProtocol.SoapEnvelopeReader.Read() at System.Xml.XmlReader.MoveToContent() at System.Web.Services.Protocols.SoapServerProtocol.SoapEnvelopeReader.MoveToContent() at System.Web.Services.Protocols.SoapServerProtocolHelper.GetRequestElement() at System.Web.Services.Protocols.Soap12ServerProtocolHelper.RouteRequest() at System.Web.Services.Protocols.SoapServerProtocol.RouteRequest(SoapServerMessage message) at System.Web.Services.Protocols.SoapServerProtocol.Initialize() at System.Web.Services.Protocols.ServerProtocol.SetContext(Type type, HttpContext context, HttpRequest request, HttpResponse response) at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing) --- End of inner exception stack trace ---

如果我会添加方法名的网址,我得到了这样的错误:

Unknown web method GetRecordJson. Parameter name: methodName Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentException: Unknown web method GetRecordJson. Parameter name: methodName

任何解决方案?

回答

2

服务器端的一些东西:

该方法不应该是静态的。 ASPX页面上的“页面方法”只是这种情况。其次,你需要用[ScriptService]属性来装饰服务类,以便能够在JSON中与它进行通信,我假设你可能想在你使用jQuery之后进行通信。

[ScriptService] 
public class BudgetJson : System.Web.Services.WebService { 
    [WebMethod] 
    public String GetRecordJson() 
    { 
    return " Hello Master, I'm Json Data "; 
    } 
} 

在客户端,你需要指定要在$.ajax()网址执行哪个方法:

$.ajax({ 
    type: "POST", 
    url: "BudgetJson.asmx/GetRecordJson", 
    data: "{}", 
    // The charset and dataType aren't necessary. 
    contentType: "application/json", 
    success: function (msg) { 
    alert(msg); 
    } 
}); 

您还可以简化$.ajax()使用了一下,如上图所示。从服务器发回的标头中的charset isn't necessary and jQuery automatically detects the dataType

+0

仍然是同样的错误... – 2011-12-13 18:00:58

相关问题