2014-09-24 26 views
0

Web服務接受application/json,但$.ajax()dataType : 'json'集合只試圖發送數據爲application/x-www-form-urlencoded。這裏有什麼竅門?錯誤415:x-www-form-urlencoded與JSON

+0

結帳這個答案http://stackoverflow.com/a/18701357/1370442注意到你需要設置的contentType以及數據類型,Ø ) – bUKaneer 2014-09-24 17:33:42

回答

2

dataType: 'json'指定jQuery希望從服務器(see docs)返回JSON 返回。爲了指定您要發送JSON,您需要添加contentType: "application/json"

此外,jQuery無法將對象轉換爲JSON。如果您傳遞任何對象data,你需要自己stringify它:

data: JSON.stringify(dataObject); 
+0

謝謝,JSON.stringify解決了今天的所有痛苦:) – 2014-09-24 17:48:09