[英]How to make a POST request using DJANGO REST framework with ajax
[英]How to make POST request using AJAX with Firebase?
我將數據保存在URL domain/data.json
,如下所示:
[{
"title": "first thought",
"content":
{
"desc":"This is the first thought",
"img":"img.png"
}
},
{
"title": "second thought",
"content":
{
"desc":"This is the second thought",
"img":"img.png"
}
}
}]
如何使用AJAX向此數據發出POST請求? 我嘗試了下面的代碼,但它給了我錯誤的請求錯誤。 我還嘗試通過chrome擴展ARC對其進行測試,但效果不佳。
$.ajax({
url: "https://musing-83d85.firebaseio.com/thoughts.json",
type: "POST",
crossDomain: true,
dataType: "json",
data: {
content:
{
desc:"This is the new thought",
img:"imgn.png"
},
title:"new title"
}, // Data sent to server, a set of key/value pairs (i.e. form fields and values)
success: function(data){ // A function to be called if request succeeds
this.setState({thought: data});
}.bind(this)
});
但這沒有用。
實際上,您的問題的解決方法非常簡單。 您現在可能已經自行解決了。
您上面發布的json文件無效。 (請參閱jsonlint.com)
同樣,在使用Firebase時,請嘗試僅使用對象。 (這是我找到了一篇很好的文章,說明了為什么https://firebase.googleblog.com/2014/04/best-practices-arrays-in-firebase.html )
您的ajax代碼看起來不錯,但是除非您發送有效的json,否則您不會獲得成功的帖子。 您也可以在鍵入代碼之前嘗試使用諸如Postman之類的工具來測試CRUD操作和json數據。 我也建議這么做。
希望這可以幫助! 干杯!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.