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

后端 未结 8 636
花落未央
花落未央 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:20

    The error indicates that the preflight is getting a redirect response. This can happen for a number of reasons. Find out where you are getting redirected to for clues to why it is happening. Check the network tab in Developer Tools.

    One reason, as @Peter T mentioned, is that the API likely requires HTTPS connections rather than HTTP and all requests over HTTP get redirected. The Location header returned by the 302 response would say the same url with http changed to https in this case.

    Another reason might be that your authentication token is not getting sent, or is not correct. Most servers are set up to redirect all requests that don't include an authentication token to the login page. Again, check your Location header to see if this is where you're getting sent and also take a look to make sure the browser sent your auth token with the request.

    Oftentimes, a server will be configured to always redirect requests that don't have auth tokens to the login page - including your preflight/OPTIONS requests. This is a problem. Change the server configuration to permit OPTIONS requests from non-authenticated users.

提交回复
热议问题