2011-12-13 48 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

相關問題