2016-01-17 34 views
0

我做錯了什麼?當我這樣做我接受任何職位變數/url

$http.post("/url", { something: "something" }) 
    .success(function(data) { 
     console.log(data); 
    }).error(function(data){ alert("An error occurred. Please try again."); } 
); 
+2

看看這個帖子:[AngularJs $ http.post()不發送數據(http://stackoverflow.com/questions/19254029/angularjs-http-post-does-not-send -data) – Fiete

+0

有趣。感謝這個@Fiete - 每天學習新東西 – bryan

回答

0

默認情況下角編碼數據Content-Type: application/json,但你希望它是Content-Type: x-www-form-urlencoded。後者默認設置爲jQuery post,這就是爲什麼大多數新人陷入陷阱。

所以你必須在做請求之前設置內容類型。

yourApp.config(['$httpProvider', function($httpProvider) { 
    $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8'; 
}]); 

現在,在每個http請求之前,內容類型會更改爲我們上面設置的內容。

在這個post中已經討論了相同的主題。

0

我有類似的HTTP頭相關的問題,在任何情況下,以下工作正常。

var data = {something: "something"}; 
$http({ 
    method: 'POST', 
    url: url, 
    data: data, 
//Uncomment this if the problem persists, it's to explicitly state the content type. 
    //headers: {'Content-Type': 'application/x-www-form-urlencoded'} 
})