我试图将用户名和密码发布到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。
以及谷歌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

10-07 19:34
查看更多