58339

Why is an OPTIONS request sent to the server?

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)

<strong>PLEASE LET ME KNOW IF YOU NEED ANY FURTHER DETAILS ABOUT THE REQUEST OR THE RESPONSE</strong>

Thanks from advance!!!

Answer1:

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

Answer2:

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

Recommend

  • How to combine or merge two sparse vectors in Spark using Java?
  • NSBundle finds mp3 file but not m4a when trying to instantiate AVAudioPlayer
  • Update Null Fields with SQL in MS Access
  • Using DOM parser in Android
  • How Lists (specifically, RecyclerView with CardViews) in Android work
  • xtable package: Skipping some rows in the output
  • How to add git credentials to the build so it would be able to be used within a shell code?
  • Android - Material Design - NavigationView - How to put vertical scroll?
  • Jackson Parser: ignore deserializing for type mismatch
  • Xcode 4 NSLog Macro link in Xcode 3
  • How to use remove-erase idiom for removing empty vectors in a vector?
  • Asynchronous UI Testing in Xcode With Swift
  • If I include Java 8 in my Android app does that affect which devices it will work on?
  • Repeat a vertical line on every page in Report Builder / SSRS
  • Deleting and Updating values from a cusrsor adapter
  • Resize panoramic image to fixed size
  • How to add date and time under each post in guestbook in google app engine
  • Volusion's generic SQL folder, functionality
  • How to set/get protobuf's extension field in Go?
  • Modifying destination and filename of gulp-svg-sprite
  • Cassandra Data Model
  • Trying to switch camera back to front but getting exception
  • 'TypeError' while using NSGA2 to solve Multi-objective prob. from pyopt-sparse in OpenMDAO
  • Importing jscolor library in angular 2
  • Circular dependency while pushing http interceptor
  • Is there a mandatory requirement to switch app.yaml?
  • bootstrap to use multiple ng-app
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • How to get icons for entities from eclipse?
  • File upload with ng-file-upload throwing error
  • ExecuteAsync RestSharp to allow backgroundWorker CancellationPending c#
  • AngularJs get employee from factory
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • How to set the response of a form post action to a iframe source?
  • How do you join a server to an Active Directory (domain)?
  • coudnt use logback because of log4j
  • Change div Background jquery
  • Turn off referential integrity in Derby? is it possible?
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • JaxB to read class hierarchy