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

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

    Please set http content type in header and also make sure the server is authenticating CORS. This is how to do it in PHP:

    //NOT A TESTED CODE
    header('Content-Type: application/json;charset=UTF-8');
    header('Access-Control-Allow-Origin: *');
    header('Access-Control-Allow-Methods: DELETE, HEAD, GET, OPTIONS, POST, PUT');
    header('Access-Control-Allow-Headers: Content-Type, Content-Range, Content-Disposition, Content-Description');
    header('Access-Control-Max-Age: 1728000');
    

    Please refer to:

    http://www.w3.org/TR/cors/#cross-origin-request-with-preflight-0

    How does Access-Control-Allow-Origin header work?

提交回复
热议问题