Cody Jonas
Cody Jonas

Reputation: 151

$.ajax vs $.post : getting allow origin error

Doing ajax using this is fine :

$.post("http://localhost:3000/scrape",
            {
                data: json
            },
            function(data, status){
            });

but this is not?

$.ajax({
              type: 'POST',
              url: 'http://localhost:3000/scrape',
              data: json,
              contentType: "application/json",
              success: function(data,status){

              },
              async:false
            });

With $.ajax I got the error of XMLHttpRequest cannot load http://localhost:3000/scrape. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

I thought both of them are the same? What's the problem here?

Upvotes: 4

Views: 61

Answers (1)

Quentin
Quentin

Reputation: 944157

In the first example you are making a simple request with application/x-www-form-urlencoded data.

In the second example you are specifying a content type which isn't one of the ones allowed for simple requests, so you are triggering a preflighted request.

The server is responding happily to the POST request, but not to the preflight OPTIONS request.

Upvotes: 4

Related Questions