我尝试将用户名和密码发布到 api,但看起来它不像 jquery post 那样简单。我不断收到此 400 错误。
代码:
$http({
method: 'POST',
url: apiLink + '/general/dologin.json',
data: {"username":"someuser","password": "somepass"}
}).success(function(response) {
console.log(response)
}).error(function(response){
console.log(response)
});
但是如果我添加这一行:
$http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
并将数据更改为:
data: "username=someuser&password=somepass"
它有效。但问题是,我必须使用 json。
来自 Google Chrome 的详细信息:
Request URL:http://coldbox.abak.si:8080/general/dologin.json
Request Method:POST
Status Code:400 Bad Request
Request Headersview source
Accept:application/json, text/plain, */*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en,sl;q=0.8,en-GB;q=0.6
Cache-Control:max-age=0
Connection:keep-alive
Content-Length:57
Content-Type:application/x-www-form-urlencoded
Host:coldbox.abak.si:8080
Origin:http://localhost:8888
Referer:http://localhost:8888/
User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36
Form Dataview sourceview URL encoded
{"username":"someuser","password":"somepass"}:
Response Headersview source
Access-Control-Allow-Origin:*
Connection:close
Content-Length:49
Content-Type:application/json;charset=utf-8
Date:Wed, 02 Apr 2014 07:50:00 GMT
Server:Apache-Coyote/1.1
Set-Cookie:cfid=b5bbcbe2-e2df-4eef-923f-d7d13e5aea42;Path=/;Expires=Thu, 31-Mar-2044 15:41:30 GMT;HTTPOnly
Set-Cookie:cftoken=0;Path=/;Expires=Thu, 31-Mar-2044 15:41:30 GMT;HTTPOnly
最佳答案
我敢打赌,如果您的 Angular 应用程序与您发布 JSON 的服务器不在同一域中,那么这就是 CORS 问题。
有关详细信息,请参阅此答案:AngularJS performs an OPTIONS HTTP request for a cross-origin resource
关于ajax - AngularJs - JSON 帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22788098/