Why my $.ajax showing “preflight is invalid redirect error”?

后端 未结 8 753
花落未央
花落未央 2020-11-28 11:04

I tried the following code in Postman and it was working. Is there something wrong with the code?

$.ajax({
   url: \'http://api.example.com/users/get\',
   t         


        
8条回答
  •  清酒与你
    2020-11-28 11:22

    This answer goes over the exact same thing (although for angular) -- it is a CORS issue.

    One quick fix is to modify each POST request by specifying one of the 'Content-Type' header values which will not trigger a "preflight". These types are:

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

    ANYTHING ELSE triggers a preflight.

    For example:

    $.ajax({
       url: 'http://api.example.com/users/get',
       type: 'POST',
       headers: {
          'name-api-key':'ewf45r4435trge',
          'Content-Type':'application/x-www-form-urlencoded'
       },
       data: {
          'uid':36,
       },
       success: function(data) {
          console.log(data);
       }
    });
    

提交回复
热议问题