Harry
Harry

Reputation: 13329

CORS issues inconsistent

Ok, Im using REST Console (Chrome extension) to test a rest API. The requests work with the REST Console, but doing the same in javascript AJAX it fails with Origin localhost:8000 is not allowed by Access-Control-Allow-Origin.

The server is setup to allow *, and I can see this in the REST Console's response headers. But looking at the my response Headers when doing a ajax request in javascript, I see that those headers is not there.

What is the difference between the two?

Here is my simple AJAX request (coffeescript)

$.ajax
    type: requestType
    contentType: "application/json"
    url: url
    data: data_string
    success: (r) =>
        successCallback(r)
    error: (r) =>
        errorCallback(r)

This works at the office in the local network, now on the VPN it does not. But REST Console always works??

Upvotes: 2

Views: 1147

Answers (1)

Paul
Paul

Reputation: 27443

application/json is not allowed for Content-Type: in a simple CORS request.

It has to be preflighted, and the server side setup for that is a bit more complex.

Try unsetting contentType or using one of these, if appropriate:

  • application/x-www-form-urlencoded
  • multipart/form-data
  • text/plain

see also my previous answer here

and a related question:

jQuery CORS Content-type OPTIONS

Upvotes: 3

Related Questions