0

次の呼び出しを実行しようとすると、解析エラーが発生します。

       $.ajax({
        cache: true,
        url: "http://localhost:3000/app/test/result1",
        data: "{}",
        type: "GET",
        jsonpCallback: "testCall",
        contentType: "application/jsonp; charset=utf-8",
        dataType: "jsonp",
        error: function (xhr, status, error) {
            alert(error);
        },
        success: function (result) {
            alert(result);
        },
        complete: function (request, textStatus) {
            alert(request.responseText);
            alert(textStatus);
        }
    });               

リクエストのURLをブラウザのアドレスバーに直接貼り付けると、返される結果は有効なJSONのようです。

    {
      "name": "The Name",
      "description": "The Description"
    }

IISnode、NodeJs、ExpressJSを使用しています。私は両方のシナリオをテストしました:

    // NODE JS CODE

    var app = express(); 

    app.get('/app/test/result1', function (req, res) {
    res.send({ name: "The Name", description: "The Description" });
    });

    app.get('/app/test/result2', function (req, res) {
    res.send(JSON.stringify({ name: "The Name", description: "the Description" }));
    });

よろしくお願いします。

4

1 に答える 1

0

dataTypeはとしてリストされていjsonpます。ただし、node.jsからの応答は、適切にフォーマットされたJSON-P応答ではありません。JSON-P応答は、次のようにコールバックでラップする必要があります。

testCall({ name: "The Name", description: "The Description" });

node.jsコードは次のようになります。

res.send(
    'testCall(' + 
    JSON.stringify({ name: "The Name", description: "the Description" }) +
    ');'
);

data: "{}"リクエストはGETリクエストであり、データが含まれていないため、この行は必要ありません。何も害はありませんが、混乱を避けるために削除するとよい場合があります)。

于 2013-01-15T04:00:40.473 に答える