为什么同一个域中Ajax请求加入JSONP回调参数?回调、参数、Ajax、JSONP

2023-09-11 01:05:57 作者:独菊

好了,这应该是一个相当简单的问题,我可能失去了一些东西明显。 我有一个简单的脚本发出请求到服务器:

Ok, this should be a fairly simple question and I am probably missing something obvious. I have a simple script making a request to the server:

var DTO = { 'path': path };
var url = 'default.aspx/Get'; 

 var test;
$('#getInstance').click(function () {
            $.ajax({
                url: url,
                type: 'POST',
                dataType: 'json',
                data: JSON.stringify(DTO),
                contentType: 'application/json; charset=utf-8',
                success: function (msg) {                    
                    test = msg;
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert(textStatus);
                    alert(errorThrown);
                }
            });

        });

此工作正常,在它连接到服务器和获取数据后面,有一个简单的问题。据处理该请求作为跨域请求,因此使用JSONP。 服务器code是在这里:

This works fine as in it connects to the server and gets the data back, with one simple problem. It is treating this request as a cross domain request, therefore using jsonp. The server code is here:

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public static MyObject Get(string path)
    {
        MyObject foo = new MyObject();

        return foo;
    }

通常,这不会是一个问题,但我访问一个WebMethod,并且它没有返回一个JSONP响应的能力(意味着它没有办法以附加回调函数的响应。现在,如果这是手动响应,我可以砍它,并附加了参数,但我趁着内置序列化,所以没办法惹周围的反应。

Normally this would not be a problem, except that I am accessing a WebMethod, and it doesnt have the capability to return a jsonp response (meaning it has no way to attach the callback function to the response. Now, if this was a manual response, I could hack it up and attach the parameter, but I am taking advantage of the built-in serialization, so no way to mess around with the response.

要帮助澄清。该网页托管在:

To help clarify. The page is hosted at:

http://127.0.0.1:144/default.aspx

和如萤火看到的请求是:

and the request as seen in firebug is:

的http://127.0.0.1:144/default.aspx/Get?callback=jQuery1502768168154247801_1298656485388

让我强调的是,这个code ++工程。唯一的问题是jQuery的处理这个请求的跨域。但是,为什么呢?

Let me just stress the fact that this code works. The only problem is jQuery treating this request as cross domain. But Why?

更新:好了,更多的时间和更多的测试后,我已经缩小这个问题到它是在jQuery的1.5.1的错误。我做了较旧的版本(所有版本1.4)的一些测试,我没有任何问题,请求使用JSON造的,响应成功接收。可能是什么,他们提出,将考虑这一请求,CORS的变化?

推荐答案

在一些调查研究,终于确定了这个问题。正如我在上一次更新,这个问题是有关使用jQuery 1.5版本。当我跑出去的想法,我想事先jQuery的版本,你会知道,它的工作正常。

After some more research, finally identified this issue. As indicated in my last update, the issue was related to using jQuery 1.5 version. As I ran out of ideas, I tried the prior jQuery version, and what would you know, it worked as expected.

当我正准备提交的bug报告,我搜索了bug数据库,发现了几个bug报告匹配相同的行为。它原来是影响新的jQuery版本jQuery的验证插件的错误。

As I was getting ready to file the bug report, I searched the bug database and found a few bug reports matching the same behavior. It turned out to be a bug in the jQuery validation plugin affecting the new jQuery version.

我发表了一篇博客条目解释