2

$.ajax() 呼び出しで Web サービスを呼び出すと期待されるデータが返され、$.post() 呼び出しで同じサービスを呼び出すと返されるデータが「未定義」である理由を誰かが理解してくれますか?

Google のツール (.post() 呼び出し用) を使用して Response を調べている間、サーバーから送り返されたデータを確認できますが、どういうわけか .post() 呼び出しの .done() 関数では使用できないことに注意してください。

プログラムはエラーなしで完了します。

コード全体は次のとおりです。

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Test01</title>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script>
        $(document).ready(function () {
            $("#btn01").click(function () {
                //.ajax type call
                $.ajax({
                    type: "POST",
                    url: "WebService1.asmx/GetData",
                    data: "{KenID:'11'}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (data)
                    {
                        $("#div01").html(data.d);
                    }
                });
            });
            $("#btn02").click(function () {
                // .post() type call
                $.post("WebService1.asmx/GetData", { KenID: "11" })
                    .done(function (data, status)
                    {
                        $("#div01").html("Data: " + data.d + "</br>Status: " + status);
                    })
                    .fail(function () { alert("error"); })
                    .always(function () { alert("completed"); });
            });
        });
    </script>
</head>
<body>
    <div id="div01">Here comes the result</div>
    <button id="btn01">Load Data with $.ajax : </button>
    <button id="btn02">Load Data with $.post : </button>
    <input id="KenID" value="11" />
</body>
</html>

ありがとうございました、

ヤセク

4

4 に答える 4

3

jsonresponse is inを定義する必要があります$.post

$.post(
    "WebService1.asmx/GetData",
    { KenID: "11" },
    function(response)
    {
        //code
    }, 'json' // <-- HERE
);
于 2013-04-01T07:38:35.810 に答える
0

以下のコード スニペットを参照してください。

$.post(""WebService1.asmx/GetData",", {id : $("#hID").val()}, function(data)
{
  doSomething( data.toString() );
});

function doSomething( data)
{
  // whatever
}
于 2013-04-01T09:02:18.053 に答える