Why is adding an extra header making the AJAX call fail

后端 未结 1 1579
一向
一向 2021-01-25 08:12

AJAX call:

$.ajax({
    url: \"http://myserver2:296/api/Demo/HelloWorld\",
    type: \"GET\",
    dataType: \'JSONP\',
    jsonp: \"callback\",
    headers: { \'         


        
相关标签:
1条回答
  • 2021-01-25 08:44

    Adding the API_KEY header to the request triggers your browser to first send a CORS preflight OPTIONS request. Any headers you add to a request other than headers defined as CORS-safelisted request-headers will trigger your browser to send a CORS preflight OPTIONS request.

    I can’t tell for sure but it seems like the 403 you’re seeing is from your server responding to that OPTIONS request, and saying it doesn’t expect to get OPTIONS requests and doesn’t allow them.

    The reason you don’t get this from Postman is that unlike browser engines, Postman does not implement CORS, so it does not send the OPTIONS request. (Postman does not operate under the same-origin Web-security model that browsers enforce for Web applications.)

    So to make your client app work as expected for scripted cross-origin access to that server, you must configure the server to respond in the right way to that CORS preflight OPTIONS request.

    0 讨论(0)
提交回复
热议问题