简体   繁体   中英

Why is same domain ajax request adding jsonp callback parameter?

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);
                }
            });

        });

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;
    }

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

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

UPDATE: Well, after many more hours and more testing I have narrowed this issue down to it being a bug in jquery 1.5.1. I did some testing with older versions (all 1.4 versions) and I had no problem, the request was made using JSON, and the response was received successfully. What could be the change they made that would consider this request a CORS?

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.

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.

I posted a blog entry with an explanation

Try explicitly setting crossDomain to false in your $.ajax() call:

$.ajax({
  crossDomain: false,
  // The rest of your options here.
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM