繁体   English   中英

如何使用AJAX和Firebase发出POST请求?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM