2013-10-14 127 views
0

所以我試圖使用jquery $.ajax作出POST請求。我的問題是發送的數據是JSON。所以我不確定如何發送它。我使用提琴手來記錄HTTP請求,這就是我得到的。 {"code":200,"user":"34522","questions":[{"35":"139"},{"55":"215"},{"28":"110"},{"88":"349"},{"127":"500"},{"148":"578"},{"125":"492"},{"218":"859"},{"258":"1019"},{"219":"862"}],"time":60}。在提琴手的網絡表單下,沒有任何東西。 enter image description here與JSON數據的POST請求

這裏是我想出了代碼和響應後面是Object {code: 500, error: "Invalid request"}

var request = $.ajax({ 
     url: "http://website/api/post/", 
     type: "POST", 
     data: { 
     code : 200, 
     user : 34522, 
     questions : '[{"35":"139"},{"55":"215"},{"28":"110"},{"88":"349"},{"127":"500"},{"148":"578"},{"125":"492"},{"218":"859"},{"258":"1019"},{"219":"862"}]', 
     time : 60 
     }, 
     dataType: "json" 
    }); 

    request.done(function(msg) { 
     console.log(msg); 
    }); 

    request.fail(function(jqXHR, textStatus) { 
     console.log("Request failed: " + textStatus); 
    }); 
+1

它在你的圖片中的字面意思是:「Content-Type是'application/json';這個Inspector只支持'x-www-form-urlencoded'。」 –

+0

試試'JSON'選項卡。 –

回答

1

嘗試在您的文章數據透過JSON.stringify這樣的:

var request = $.ajax({ 
    url: "http://website/api/post/", 
    type: "POST", 
    data: JSON.stringify({ 
    code : 200, 
    user : 34522, 
    questions : [{"35":"139"},{"55":"215"},{"28":"110"},{"88":"349"},{"127":"500"},{"148":"578"},{"125":"492"},{"218":"859"},{"258":"1019"},{"219":"862"}], 
    time : 60 
    }), 
    dataType: "json" 
}); 

JSON.stringify將當前的JavaScript對象轉換爲JSON字符串張貼。還要注意從questions數組周圍刪除撇號。

+0

非常感謝你! –

0

的數據類型:JSON指的是我相信的響應。所以如果服務器響應不是有效的json,你會看到一個500錯誤。

試着看看firebug中的'network(chrome)'或'net(firefoxl)'選項卡來檢查響應。

您也可以嘗試改變dataType:jsondataType:text