好吧,当我在谷歌浏览器上使用 angularjs 的 $http.post 时,我遇到了这个问题,它总是返回错误 400,这是错误的请求,但是当我尝试时Mozilla Firefox 工作正常!
这是$http服务代码
$http({
'method': "POST",
'url': "http://example.com",
'data': data,
'withCredentials': true,
'cache': false,
headers: {
'Content-type': 'application/json',
'authTokens': sessionServices.get('userLogin').userLoginTokens,
'JSESSIONID': sessionServices.get('JSESSIONID')
}
}).
success(function(data, response, headers, status) {
console.log("sucess");
}).
error(function(data, response) {
console.log("failed");
});
这是 $http 请求本身的数据输入示例
data = [123415, 32324123, 1123124123, 1213123]
注意*
我提交的后端 URL 位于另一个域中,因此例如我将应用程序部署在 http://example1.com 上当后端处于 http://example.com 时,起初我以为这是 CORS 问题,但显然我传递了 OPTION 调用,当移动到第二个调用(即 POST 请求)时,它返回错误 400
显然 chrome 忽略了我的自定义 header 选项集,即“application/json”,但在 chrome 中它返回的结果不同。您可以在下面看到返回的调用
远程地址:example1.com 请求网址:example.com 请求方式:POST 状态代码:400 错误请求
Request Headers
view source
Accept:application/json, text/plain, */*
Accept-Encoding:gzip,deflate
Accept-Language:en-US,en;q=0.8,id;q=0.6,ms;q=0.4
authTokens:13f4ca0d-eb30-435b-b313-a78ed8fff5ef
Connection:keep-alive
Content-Length:313
Content-Type:application/json;charset=UTF-8 application/json
Cookie:JSESSIONID=F524FAFC1178EE3451A7D82CBAD9BE87
Host:http://example1.com
JSESSIONID:F524FAFC1178EE3451A7D82CBAD9BE87
Origin:http://example.com
Referer:http://example.com
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.124 Safari/537.36
X-Requested-With:XMLHttpRequest
Request Payload
view source
[d6fece6d-fd4b-4377-b326-7e7cdafbe02b, 6240a840-29a7-4e16-899a-2db0fde6ffdd,…]
0: "d6fece6d-fd4b-4377-b326-7e7cdafbe02b"
1: "6240a840-29a7-4e16-899a-2db0fde6ffdd"
2: "794ae064-38f3-4324-8cda-5afaabe2347d"
3: "41d28490-624d-418d-8fda-4a0ed8c65aa2"
4: "38e06d09-1d33-42d2-b6a2-e06548743a1a"
5: "7f8e92c8-4217-443a-9830-3da78a75b1a6"
6: "ed31b19a-2b2a-4fd7-99b8-b5b61d6a881f"
7: "2b1b8636-d342-4114-9de3-40d42003790f"
Response Headers
view source
Access-Control-Allow-Credentials:true
Access-Control-Allow-Origin:example1.com
Access-Control-Expose-Headers:Jsessionid
Connection:close
Content-Language:en
Content-Length:990
Content-Type:text/html;charset=utf-8
Date:Wed, 15 Oct 2014 15:25:48 GMT
Server:Apache-Coyote/1.1
Vary:Origin
你也有这个问题吗?你们能帮我吗?
最佳答案
可能是因为 Content-Type
header 不正确:
Content-Type:application/json;charset=UTF-8 application/json
您的 JavaScript 代码中有一个拼写错误:
headers: {
'Content-type': 'application/json',
它必须是 -Type
,大写 T。无论如何,默认值为 application/json
,因此您也可以删除该行。
Chrome 可能会合并 Content-type
和 Content-type
,而 Firefox 会发送两个单独的 header 或仅发送其中一个 header 。
关于javascript - $http post在谷歌浏览器上返回400,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26387035/