0

だから私はいくつかの問題を抱えています。問題の目標は、Ajax を使用してユーザー コントロールから 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: サーバーは要求を処理できませんでした。---> System.Xml.XmlException: ルート レベルのデータが無効です。行 1、1 の位置。 System.Xml.XmlTextReaderImpl.Read() での ParseDocumentContent() System.Xml.XmlTextReader.Read() で System.Web.Services.Protocols.SoapServerProtocol.SoapEnvelopeReader.Read() で System.Xml.XmlReader.MoveToContent() System.Web.Services.Protocols.SoapServerProtocol.SoapEnvelopeReader.MoveToContent() で System.Web.Services.Protocols.SoapServerProtocolHelper.GetRequestElement() で System.Web.Services.Protocols.

メソッド名をURLに追加すると、次のようなエラーが発生しました:

不明な Web メソッド GetRecordJson。パラメーター名: methodName 説明: 現在の Web 要求の実行中に未処理の例外が発生しました。エラーの詳細とコード内のどこでエラーが発生したかについては、スタック トレースを確認してください。

例外の詳細: System.ArgumentException: 不明な Web メソッド GetRecordJson。パラメータ名: methodName

解決策はありますか?

4

1 に答える 1

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()クライアント側では、実行するメソッドを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 は必要なく、jQuery はサーバーから返されたヘッダーから dataType を自動的に検出します。

于 2011-12-13T17:56:53.207 に答える