1

ASP.NET ベースの jQuery モバイル Web サイトから Web サービスを呼び出そうとしています。Web サービスは post パラメータを受け取り、XML を返します。Web サービス側を変更できません。

クロスドメインの問題に直面しています。私の理解では、プロキシを使用する必要があります。

汎用ハンドラーを使用することを考えていますが、その方法がわかりません。

私のサービスメソッドは次のようになります:

https://myserver.com/WCF/Info.svc/MyMehod1
https://myserver.com/WCF/Info.svc/MyMehod2

そしてポストパラメータを取る

C#でそれを行うにはどうすればよいでしょうか?

ありがとう

4

2 に答える 2

2

この質問をチェックしてください: jQuery から Web サービスにアクセスする - クロスドメイン

別の方法として、jQuery Ajax で呼び出す HttpHandler を作成することもできます。その後、ハンドラーは Web サービスを呼び出して、出力を Http 応答に書き込むことができます。

于 2012-10-09T20:17:24.023 に答える
1

私はついにそれを働かせました。

同じ悩みを抱えている方へ、

クライアント側では、一般的なハンドラーを使用して Web サービス呼び出しを行い、結果を公開しました。

ハンドラーのサンプル:

public void ProcessRequest(HttpContext context)
{
    string method = context.Request.QueryString["method"].ToString().ToLower();

    if (method == "MyMethod1")
    {
        context.Response.ContentType = "text/plain";
        context.Response.Write(CallWebService(method, param));
        context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
    }
    else if (method == "MyMethod2")
    {
        context.Response.ContentType = "text/plain";
        string param = "myparam";
        context.Response.Write(CallWebService(method, param));
        context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
    }
    else
    {
    //context.Response.ContentType = "text/plain";
    //context.Response.Write("Hello World");
    }


}

private string CallWebService(string method, string param)
{
    string ServeurURL = "https://myserver.com"; 

    System.Net.WebRequest req = System.Net.WebRequest.Create(ServeurURL + method);
    req.Method = "POST";

    byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(param);
    req.ContentType = "text/xml";
    req.ContentLength = byteArray.Length;
    System.IO.Stream reqstream = req.GetRequestStream();
    reqstream.Write(byteArray, 0, byteArray.Length);
    reqstream.Close();

    System.Net.WebResponse wResponse = req.GetResponse();
    reqstream = wResponse.GetResponseStream();
    System.IO.StreamReader reader = new System.IO.StreamReader(reqstream);
    string responseFromServer = reader.ReadToEnd();

    return responseFromServer;
}

jQuery/Ajax 呼び出し:

jQuery(function() {
        $('#btn1').click(function (e) {
            e.preventDefault();
            jQuery.ajax({
                type: "GET",
                url: "MyHandler.ashx",
                data: "method=MyMethod1",
                success: function (data) {
                    $('#display').html("<h1>" + data.toString() + "</h1>");
                }
            });
        });
        $('#btn2').click(function (e) {
            e.preventDefault();
            jQuery.ajax({
                type: "GET",
                url: "MyHandler.ashx",
                data: "method=MyMethod2",
                success: function (data) {
                    $('#display').html("<h1>" + data.toString() + "</h1>");
                }
            });
        });
    });

そして今、それは働いています:)

于 2012-10-10T15:54:55.920 に答える