3

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?

9
  • Did you try the full uri? (i.e. http://domain.com/default.aspx/Get) Commented Feb 25, 2011 at 17:50
  • @artyom.stv Yes, and I can see it in Firebug as the same domain. Edited question to show as such. Commented Feb 25, 2011 at 17:53
  • Try to remove charset=utf-8. Is it possible? Commented Feb 25, 2011 at 18:16
  • @Victor - I'm not sure, can you use {data:...} option with a string (not with the object as the key/value pairs). I didn't use jQuery this way (with {data: "string"}). Commented Feb 25, 2011 at 18:23
  • @artyom.stv It has to be stringified for it to work with the server. Otherwise the server does not recognize the data. Commented Feb 25, 2011 at 18:25

2 Answers 2

9

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

Sign up to request clarification or add additional context in comments.

2 Comments

God I just spent the last 30 minutes with this problem as well. TY Victor for finding out the reason why
Warning! Google Chrome is telling me: "www.victorocastro.com contains malware. Your computer might catch a virus if you visit this site."
0

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

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

1 Comment

This only works to force a crossDomain (jsonp) request for a local domain.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.