2011-12-13 21 views
0

が返されるため、問題が発生します。問題の目標は、Ajaxを使用してユーザーコントロールからWebサービスを呼び出そうとすると、500の内部サーバーエラーが発生することです。ASP.NET - ユーザーコントロール(.Ascx)からWebサービスを呼び出すとエラー500内部サーバーエラー

私のコードサンプルがあります:

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)(アヤックスコール)ファイル

Webサービス.CSので

$(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 ---

私はURLにメソッド名を追加する場合は、私は、このようなエラーを得た:

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ページの「ページメソッド」の場合のみです。

第2に、JSONで通信できるように、[ScriptService]属性のサービスクラスをデコレートする必要があります。これは、jQueryを使用しているので、おそらくやりたいことです。クライアント側では

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

、あなたはあなたの$.ajax() URLで実行したいメソッドを指定する必要があります。

$.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

まだ同じエラー... –

+0

@AlekoGharibashvili私が最初に逃したクライアント側の問題がありました。私の答えを更新しました。 –

+0

クライアントサイドスクリプトも更新されましたが、このコードはあなたのマシンで動作しますか?いくつかの設定に問題がありますか?このコードがあなたのマシンで実行されている場合は、web.configファイルを共有してください... –

関連する問題