0

A server is looking for CORS requests to get data sets, but the issue is that i dont know how to create a proper CORS request using $.ajax.

There is 1 Stackoverflow post which helps describe it: How to get a cross-origin resource sharing (CORS) post request working

There is a Wikipedia Article on it: http://en.wikipedia.org/wiki/Cross-origin_resource_sharing

But all in all, despite it telling me that information, i have issues understanding how to properly do the request.

In the first link, it just does a

$.post(SERVER_URL, {file_url:"asfd"}, function(e){console.log(e);});

I think my confusion is that i was thinking that i would need to do 2 requests. One which does the initial hand-shaking, and then the other which takes the result of the first plus some other data.

Could someone help me create a proper CORS requet using jQuery OR Javascript? It seems like i dont quite understand how the structure of it is. If there are certain flags i need to use. I am used to doing simple JSONP requests, which since it is not CORS get the data, but then fail.

Thank you.

EDIT I have a post which is related that was resolved. The Result was JSON, not JSONP, but the server department said CORS was set up on the machines so i figured i did the syntax wrong. Here is the link: AJAX request returns 200OK but fails despite return data being JSON

Community
  • 1
  • 1
Fallenreaper
  • 10,222
  • 12
  • 66
  • 129
  • The browser does that for you automatically. – SLaks Jul 22 '13 at 18:07
  • So if i do an **$.ajax()** request, it will auto do CORS if JSONP doesnt work? It seeems like when i was doing my ajax call, it returned ERROR even though the errorcode was success. I figured it didnt do it via CORS, and just did it as JSONP. – Fallenreaper Jul 22 '13 at 18:12
  • The server has to support CORS for CORS to work. – John Dvorak Jul 22 '13 at 18:19

1 Answers1

2

I suggest going through the MDN Documentation: https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS

Both theory and practical examples are explained.

David Riccitelli
  • 7,491
  • 5
  • 42
  • 56