Ted B.
Ted B.

Reputation: 65

Sending query strings with $.ajax type OPTIONS

My REST API developer told me to check if a file is available for download before going to the actual link by first doing an ajax call using OPTIONS type.

If it returns OK, then I can go ahead and redirect the browser to that page and download the file.

return $.ajax({
  type: 'OPTIONS',
  url: 'http://api/account-data',
  data: {user_id: '5'}
}).done(function(data) {
  // redirect browser to http://api/account-data?user_id=5
});

The problem I encountered is that I can't attach any query strings when I use OPTIONS. Also, I've never heard of anyone using OPTIONS before. From what I've read, the browser does this automatically when doing certain cross-origin calls. I told him this, but I was told to search for a solution to attach the query strings. Is there a way?

Upvotes: 0

Views: 52

Answers (1)

Quentin
Quentin

Reputation: 943108

Also, I've never heard of anyone using OPTIONS before.

People are generally pretty bad about using HTTP correctly.

That said, I would expect to use a HEAD request to determine is a resource was available or not.

From what I've read, the browser does this automatically when doing certain cross-origin calls.

The CORS spec mandates an OPTIONS request as a pre-flight request under some circumstances, but OPTIONS predated CORS and there are other reasons to use that HTTP verb.

I was told to search for a solution to attach the query strings. Is there a way?

jQuery ignores data when you make an OPTIONS request. Put the query string directly in the URL.

$.ajax({
  type: 'OPTIONS',
  url: 'http://api/account-data?user_id=5'
})

Upvotes: 1

Related Questions