2

I use a simple jQuery.ajax method:

    $.ajax({
        type: "GET",
        url: serviceUrl + '/GetAgentsGroupNameById',
        contentType: "application/json; charset=utf-8",
        data: { id: agentsGroupId },
        async: false,
        success: function (data) {
            agentsGroupName = data["d"];
        },
        error: function (request, message) {
            agentsGroupName = '';
        }
    });

The 'Get' request is sent and I get a well-formed json response from the server. The problem is that I see in the developer tools that another request is generated to the same URL, with request method: OPTIONS, with an empty response, and I see an error:

OPTIONS http://localhost:1004/MobileService.asmx/GetSubscribedAgentsByUserId?userId=27 500 (Internal Server Error) 

What is this OPTIONS request? Why does it happen?

P.S. I mentioned that if I delete contentType: "application/json; charset=utf-8" (and add dataType: json or jsonp), no OPTIONS request is generated, but I don't get a well-formed json as a response (I get kinda xml document)

BTW: the service is asp.net c#:

[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public string GetAgentsGroupNameById(int id)

PLEASE LET ME KNOW IF YOU NEED ANY FURTHER DETAILS ABOUT THE REQUEST OR THE RESPONSE

Thanks from advance!!!

Sagnik Sinha
  • 873
  • 1
  • 11
  • 22
benams
  • 4,308
  • 9
  • 32
  • 74

2 Answers2

2

The OPTIONS request is because of the Cross origin resource sharing. It is basically the web browser asking the destination server whether it allows cross domain resource sharing.

In short, you cannot make json requests to a different domain than the domain that the page is being served from.

If you are only doing GET requests, you may want to look at JSONP which solves this issue. However, it only works with GET requests.

There have been a lot of questions on this, and there is a details answer here

Community
  • 1
  • 1
David Masters
  • 8,069
  • 2
  • 44
  • 75
  • Not sure where that link is supposed to take people though?! – David Masters Jul 11 '12 at 14:50
  • I only do "GET" request, as you can see, and when I use dataType:'jsonp', I have no errors, but I get an xml document as a response, instead of a json object. – benams Jul 11 '12 at 14:52
  • @DavidMasters Oops, wrong link :p This is the one I intended to use: https://developer.mozilla.org/en/http_access_control. – Rob W Jul 11 '12 at 14:55
  • btw, the service I use has a response header of "Access-Control-Allow-Origin: *", so I'm not sure if the cross origin resource is the problem – benams Jul 11 '12 at 14:55
  • @benams Hmm interesting. Do you get xml back even when you have set contentType to application/json & dataType to jsonp? – David Masters Jul 11 '12 at 15:01
  • yeah.. here is what i get back: Sports. Maybe something wrong with the webservice itself? – benams Jul 11 '12 at 15:06
  • @DavidMasters I can see now in the developer tools that the response has an header of "Content Type: text/xml; charset=utf-8" although I set the contentType to be application/json – benams Jul 11 '12 at 15:13
  • Are you talking about the OPTIONS request or your GET request? The browser will make the OPTIONS request before the GET by itself ignoring the headers you've specified... – David Masters Jul 11 '12 at 15:16
  • when I set dataType: 'jsonp' I see no OPTIONS request in the developer tools. I attach a link to a printscreen of the developer tools with the request and response's details: http://i50.tinypic.com/w9h11j.png – benams Jul 11 '12 at 15:21
  • @DavidMasters [forgot to tag you in my last comment...] – benams Jul 11 '12 at 15:44
  • To be honest, I don't know why you are getting xml back. I haven't actually used jsonp myself because I need POST requests. Maybe ask another question titled 'Why do I get an XML response from a JSONP request'. But I think I've answered your original question regarding why OPTIONS requests occur. – David Masters Jul 11 '12 at 15:49
  • Ok, I'll try it. THANK YOU! and one more thing: you told that you use POST requests, you can get json response like that?? @DavidMasters – benams Jul 11 '12 at 16:30
  • I have modified my web service to add the required CORS headers which means my post requests work. So when I make an ajax request the browser makes an OPTION request first... – David Masters Jul 11 '12 at 16:35
-1

Try switching your $.ajax 'Type' to use the POST verb rather than GET.

Brian Scott
  • 9,221
  • 6
  • 47
  • 68