0

大家好,下面是我的代码

$.ajax({
    'url': 'myUrl',
    'dataType': 'jsonp',
    crossDomain: true,
    'data': {
      // sample daa
    },
    'success': function (data) {
      alert("here::");
    },
    'error': function (jqxhr, status, errorThrown) {
      alert("Failure, Unable to recieve content : " + status)
      alert(jqxhr.responseText);
    })
})

我正在使用 jsonp,因为我的 url 属于其他域,并且我能够将响应代码设为 200,并且我可以看到响应中的数据(在 Firebug 中检查),但没有一个成功或错误方法正在执行。
请帮助我

更新响应(从萤火虫复制)

{"documents":[{"trans":"sdsd","orig":"How","translit":"Elā","src_translit":""}],"dict":[{"pos":"unknown","terms":["dgssdg","sgsd"],"entry":[{"word":"gsdg","reverse_translation":["method","treatment","recipe","attitude","how","retro"],"score":0.000305442198},{"word":"మార్గము","reverse_translation":["way","route","road","entry","how","impasse"],"score":0.000305442198}],"base_form":"how","pos_enum":20}],"src":"en","server_time":12}
4

2 回答 2

0

如果您想使用 jsonp,您的代码必须如下所示:

示例客户端

    $.ajax({
        dataType    : "jsonp",
        url         : ' Some url ',
        data        : { some json data },
        // this is jsonp callback that use must to return from server side
        jsonpCallback : 'getResponse' 
    });

服务器端必须返回如下内容:

echo "getResponse({ some json data })";
于 2013-08-02T07:54:00.290 回答
0

这不起作用,因为您的服务器端脚本显然返回的是 JSON 而不是 JSONP。如果您希望能够使用跨域 AJAX 请求调用它,则应修改您的服务器端脚本,以便它返回 JSONP。

您可以在此处阅读有关 JSONP 的更多信息:http ://en.wikipedia.org/wiki/JSONP

所以而不是:

{
    "documents": [
        {
            "trans": "sdsd",
            "orig": "How",
            "translit": "Elā",
            "src_translit": ""
        }
    ],
    "dict": [
        {
            "pos": "unknown",
            "terms": [
                "dgssdg",
                "sgsd"
            ],
            "entry": [
                {
                    "word": "gsdg",
                    "reverse_translation": [
                        "method",
                        "treatment",
                        "recipe",
                        "attitude",
                        "how",
                        "retro"
                    ],
                    "score": 0.000305442198
                },
                {
                    "word": "మార్గము",
                    "reverse_translation": [
                        "way",
                        "route",
                        "road",
                        "entry",
                        "how",
                        "impasse"
                    ],
                    "score": 0.000305442198
                }
            ],
            "base_form": "how",
            "pos_enum": 20
        }
    ],
    "src": "en",
    "server_time": 12
}

您的服务器端脚本应返回:

someCallback({
    "documents": [
        {
            "trans": "sdsd",
            "orig": "How",
            "translit": "Elā",
            "src_translit": ""
        }
    ],
    "dict": [
        {
            "pos": "unknown",
            "terms": [
                "dgssdg",
                "sgsd"
            ],
            "entry": [
                {
                    "word": "gsdg",
                    "reverse_translation": [
                        "method",
                        "treatment",
                        "recipe",
                        "attitude",
                        "how",
                        "retro"
                    ],
                    "score": 0.000305442198
                },
                {
                    "word": "మార్గము",
                    "reverse_translation": [
                        "way",
                        "route",
                        "road",
                        "entry",
                        "how",
                        "impasse"
                    ],
                    "score": 0.000305442198
                }
            ],
            "base_form": "how",
            "pos_enum": 20
        }
    ],
    "src": "en",
    "server_time": 12
})

客户端应该能够通过将回调函数 ( someCallback) 作为查询字符串参数传递来指定回调函数的名称。

然后您可以执行跨域 AJAX 调用:

$.ajax({
    url: 'http://example.com/some_endpoint',
    jsonp: 'callback',
    dataType: 'jsonp',
    data: { firstName: 'john', lastName: 'smith' },
    success: function (result) {
        alert(result);
    },
    error: function (jqxhr, status, errorThrown) {
        alert('Failure, Unable to recieve content : ' + status)
        alert(jqxhr.responseText);
    }
});
于 2013-08-02T07:44:34.287 回答