Javascript 为什么我的 $.ajax 显示“预检无效重定向错误”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33645511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why my $.ajax showing "preflight is invalid redirect error"?
提问by Jennifer Aniston
I tried the following code in Postmanand it was working. Is there something wrong with the code?
我在Postman 中尝试了以下代码并且它正在工作。代码有问题吗?
$.ajax({
url: 'http://api.example.com/users/get',
type: 'POST',
headers: {
'name-api-key':'ewf45r4435trge',
},
data: {
'uid':36,
},
success: function(data) {
console.log(data);
}
});
I got this error in my console as below, please advise.
我在控制台中收到此错误,如下所示,请指教。
XMLHttpRequest cannot load http://api.example.com/users/getResponse for preflight is invalid (redirect)
XMLHttpRequest 无法加载http://api.example.com/users/get预检响应无效(重定向)
回答by Peter T.
I received the same error when I tried to call https web service as http webservice.
当我尝试将 https web 服务称为 http web 服务时,我收到了同样的错误。
e.g when I call url 'http://api.example.com/users/get'
which should be 'https://api.example.com/users/get'
This error is produced because of redirection status 302 when you try to call http instead of https.
当您尝试调用 http 而不是 https 时,此错误是由于重定向状态 302 而产生的。
回答by JoshuaDavid
This answergoes over the exact same thing (although for angular) -- it is a CORS issue.
这个答案涉及完全相同的事情(尽管是角度问题)——这是一个 CORS 问题。
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:
一种快速解决方法是通过指定不会触发“预检”的“Content-Type”标头值之一来修改每个 POST 请求。这些类型是:
- application/x-www-form-urlencoded
- multipart/form-data
- text/plain
- 应用程序/x-www-form-urlencoded
- 多部分/表单数据
- 文本/普通
ANYTHING ELSE triggers a preflight.
ANYTHING ELSE 触发预检。
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);
}
});
回答by Planky
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.
正如@Peter T 提到的,原因之一是 API 可能需要 HTTPS 连接而不是 HTTP,并且所有通过 HTTP 的请求都会被重定向。在这种情况下Location
,302
响应返回的标头会说与http
更改为相同的 url https
。
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.
另一个原因可能是您的身份验证令牌未发送或不正确。大多数服务器都设置为将所有不包含身份验证令牌的请求重定向到登录页面。再次检查您的Location
标头,看看这是否是您被发送的地方,并确保浏览器将您的身份验证令牌与请求一起发送。
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.
通常,服务器将配置为始终将没有身份验证令牌的请求重定向到登录页面 - 包括您的预检/OPTIONS
请求。这是个问题。更改服务器配置以允许OPTIONS
来自未经身份验证的用户的请求。
回答by Airwind711
Please set http content type in header and also make sure the server is authenticating CORS. This is how to do it in PHP:
请在标头中设置 http 内容类型,并确保服务器正在验证 CORS。这是在 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
http://www.w3.org/TR/cors/#cross-origin-request-with-preflight-0
回答by ehacinom
My problem was that POST requests need trailing slashes '/'.
我的问题是 POST 请求需要尾部斜杠“/”。
回答by Chambah Russell
I had the same problem and it kept me up for days. At the end, I realised that my URL pointing to the app was wrong altogether. example:
我有同样的问题,它让我好几天。最后,我意识到我指向该应用程序的 URL 完全错误。例子:
URL: 'http://api.example.com/'
URL: 'https://api.example.com/'.
If it's http or https verify.
Check the redirecting URL and make sure it's the same thing you're passing along.
如果是 http 或 https 验证。
检查重定向 URL 并确保它与您传递的内容相同。
回答by Bolli
I had the same error, though the problem was that I had a typo in the url
我有同样的错误,但问题是我在 url 中有一个错字
url: 'http://api.example.com/TYPO'
The API had a redirect to another domain for all URL's that is wrong (404 errors).
API 将所有错误的 URL 重定向到另一个域(404 错误)。
So fixing the typo to the correct URL fixed it for me.
因此,将错字修正为正确的 URL 为我修复了它。
回答by user558720
My problem was caused by the exact opposite of @ehacinom. My Laravel generated API didn't like the trailing '/' on POST requests. Worked fine on localhost but didn't work when uploaded to server.
我的问题是由@ehacinom 的完全相反引起的。我的 Laravel 生成的 API 不喜欢 POST 请求中的尾随“/”。在本地主机上运行良好,但在上传到服务器时不起作用。