0

So I have a jQuery ajax request, and when it fires I get the Access-Control-Allow-Origin problem. The server I'm sending it to is not under our management, but is an API that is supposed to accept POST requests.

What's really weird is that even when I get this error, if I go to Chromes Network tab, click on the request and then Preview or Response, I see the data I need.

Is there no way to get this data from the request since ajax is saying it failed?

I can't do jsonp by the way.

ajax call

$.ajax({
        url: url,
        type: 'POST',
        data: body,
        dataType: 'json',
        success: function(data) {
            console.log(data);
        },
        error: function(xhr, status, error) {
            console.log(xhr.responseText);
            console.log(status);
            console.log(error);
        },
        complete: function(xhr, status){
            console.log(xhr, status);
        }
    });

The browser code is 200, and I can see the data I need to access right there in the tab. But it's no where in the console from all this.

Octoxan
  • 1,949
  • 2
  • 17
  • 34
  • There is no way for you to get the data in JS. The request still completes, which is why you can see the response in the dev tools, but the browser's security will prevent the data being passed to JS for processing. If you need to access an API which does not include CORS headers in the response, such as in this case, you will need to make the request to it from the server side. – Rory McCrossan Jul 19 '18 at 19:16
  • @RoryMcCrossan I understand there being no solution, but this question has nothing to do with Postman. Also, the answer in there is to correctly set CORS on the server... which if you read the question, isn't something we have access to as it's a 3rd party API. If someone ends up here from this question and follows the link, it won't help them at all. – Octoxan Jul 19 '18 at 19:18
  • The answer is 'you can't from JS - do it server side', which is why I left the above comment. The closure link to that duplicate is so you can educate yourself for future reference. – Rory McCrossan Jul 19 '18 at 19:21

0 Answers0