我試圖將用戶名和密碼發佈到api,但看起來不像jquery post那麼簡單。我一直在製造這個400錯誤。AngularJs - JSON post
代碼:
$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。從谷歌瀏覽器
和詳細信息:
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
如果您已經解決了您的問題;你有問題嗎?您可以設置一個轉換函數,因此您不必擔心自動執行轉換。更多信息請點擊http://www.jeffryhouser.com/index.cfm/2013/10/1/Calling-a-ColdFusion-CFC-from-AngularJS並在這裏http://victorblog.com/2012/12/20/ make-angularjs-http-service-behave-like-jquery-ajax/ – JeffryHouser
我的猜測是這是一個服務器端問題。角碼看起來正確。也許服務器沒有配置爲處理json參數? – alexsanford1