-1

All, I have the following ajax request that works fine in IE (I know right??) but when I try to use the same code in Chrome I get the following error:

XMLHttpRequest cannot load http://ahmwpsds01:1234/NASAService/GetNASAUser.asmx/GetUserByName. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://ahmwpsds01' is therefore not allowed access. The response had HTTP status code 500.

Ajax Request

function getNASAProfile(user) {
    var dfd = new $.Deferred(function () {
        $.support.cors = true;
        $.ajax({
            type: "POST",
            url: "http://ahmwpsds01:1234/NASAService/GetNASAUser.asmx/GetUserByName",
            data: "{userName: '" + UserProfile.Name + "'}",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            accept: "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
        })
        .done(function (data) {
            //alert('Call Succeeded');
            dfd.resolve(data.d);
        })
        .fail(function (jqXHR, textStatus, errorThrown) {
            alert('Call Failed\n\n' + jqXHR.statusText + "\n\n" + errorThrown + "\n\n" + textStatus);
        });
    });
    return dfd.promise();
}

Any thoughts?

JD Sanders
  • 82
  • 1
  • 12
  • Yup, your typical CORS error. IE is incorrectly allowing it, or not sending an OPTIONS preflight request that should be required with the given request. – Kevin B Sep 11 '15 at 19:55
  • It's exactly what it says it is. The url you are requesting needs a Access-Control-Allow-Origin header. https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Access-Control-Allow-Origin – danielrsmith Sep 11 '15 at 19:56
  • No I don't think so since I am sending $.support.cors and it works in IE. – JD Sanders Sep 11 '15 at 19:57
  • @JDSanders it's clearly a CORS error, read the error message. Research what CORS means and how it works. Your server isn't responding to the CORS request in such a way that would allow the browser to make the request successfully. – Kevin B Sep 11 '15 at 20:01
  • Also, the JSON you are posting is invalid, fyi. your server might not care though. – Kevin B Sep 11 '15 at 20:02

1 Answers1

0

EDIT: Didn't notice that you want to make a POST request. You should consider using a backend method for your case

You are trying to access a resource that is on a different server. For security reasons, you are not allowed to do that with a simple AJAX call.

To work out the problem, you can use JSONP. The simple way to do it is to add ?callback=? to the URL. Plus, the server response must be wrapped in a function definition. You might want to use a proxy like YQL to convert the response to JSONP.

More information on this link : http://json-jsonp-tutorial.craic.com/index.html

Lyes BEN
  • 990
  • 4
  • 14