【问题标题】:Jquery posts not working with flask-restless, where curl -H posts areJquery 帖子不适用于 flask-restless,其中 curl -H 帖子是
【发布时间】:2012-06-26 22:44:48
【问题描述】:

我安装了一个烧瓶应用程序,运行烧瓶-restless API 以下命令执行成功:

curl -H "Content-type: application/json" -X POST http://127.0.0.15000/api/menu -d '{"name":"Hello Data"}'

但是当试图用 javascript 做同样的事情时,它失败了。我已经将其追溯到flask-restless api中的问题代码行,并添加了一个打印出request.data的调试语句,因为它依赖于它。

最接近工作的 jquery 查询是:

$.ajax({
url:'api/menu', 
type:"POST",

contentType:"binary/octet-stream",

data:{name:'foo'}});

为哪个 request.data 分配“name=foo”,它应该是 {'name':'foo'}

我已经尝试过 (contentType: "application/json", dataType: "json") 的所有排列 对于内容类型 application/json, binary/octet-stream, 在所有其他情况下,数据只是空白,并且 request.form 有一个不可变的字典。

我有什么明显的遗漏吗?

【问题讨论】:

    标签: jquery flask


    【解决方案1】:

    糟糕 - 数据 - 没有以 json 格式发送 你必须这样做

    $.ajax({
        url:'http://localhost:5000/api/menu', 
        type:"POST",
    
        contentType:"binary/octet-stream",
        host:"localhost",
        data:JSON.stringify({"name":"foo"})});
    

    注意 JSON.stringify !!!

    【讨论】:

      猜你喜欢
      • 2010-11-22
      • 2015-05-24
      • 1970-01-01
      • 1970-01-01
      • 2015-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多