2016-02-04 103 views
0

在过去的3个小时,我一直在努力使一个Firefox插件从main.js/index.js页面上的PUT请求没有运气。AJAX Put请求从附加SDK

从技术文档,这是正确的AJAX调用(用正确的网站,如果你想尝试编辑)

$.ajax({ 
    url:"https://api.myjson.com/bins/1ihp9", 
    type:"PUT", 
    data:'{"key_updated":"value_updated"}', 
    contentType:"application/json; charset=utf-8", 
    dataType:"json", 
    success: function(data, textStatus, jqXHR){ 

    } 
}); 

Firefox插件的SDK不使用$.ajax(),而是使用Request()。所以,我做的代码是这样的:

var latestTweetRequest = Request({ 
    url: "https://api.myjson.com/bins/1ihp9", 
    //updatedjson is a string here, as requested in documentation 
    content: updatedjson, 
    headers: { 
     "contentType": "application/json; charset=utf-8", 
     "dataType": "json" 
    }, 
    onComplete: function (response) { 
     console.log(response); 
    } 
}); 
latestTweetRequest.put(); 

但无论我做什么,它都不起作用。响应隆重推出“构造{})

我可以做一个GET请求到现场就好了。

+0

您是否在网络标签中检出了请求?是否有任何错误代码返回?电话甚至会外出?你有没有试过一个标准的get,看看你是否可以接收数据? – scrappedcola

回答

0

可悲的是请求对象不会让你处理错误,所以当事情出错,你只是在想,你可以尝试使用net/xhr模块,如果你想要更多的控制。

我不知道这个问题是在这里什么,但它似乎是一个服务器不喜欢是看到的对象。

我发现这个错误消息被返回(作为JSON对象):

{ 
status:500 
message:"Internal Server Error" 
more_info:"undefined method `string' for #<PhusionPassenger::Utils::TeeInput:0x007fcf73b9bd98>" 
} 

更重要的是,这里有一个简单的黑客来帮助你自己调试。

var { setTimeout } = require("sdk/timers"); 
setTimeout(function() { 
    var latestTweetRequest = Request({ 
      url: "https://api.myjson.com/bins/1ihp9", 
      content: updatedjson, //updatedjson is a string here, as requested in documentation 
      headers: { 
       "contentType" : "application/json; charset=utf-8", 
       "dataType" : "json" 
      }, 
      onComplete: function (response) { 
      console.log(response); 
      }, 
      onError: function (err) { 
      console.error(err); 
      } 
     }); 
     latestTweetRequest.put(); 

}, 1000 * 20); 

该代码给出在20秒延迟计时器中包装您的请求。从Firefox启动并运行这个附加代码开始,这是20秒,所以没有太多时间,但足以完成此任务。

一旦通过您的jpm run命令启动Firefox。立即转到工具菜单。打开工具 - > Web开发人员 - >浏览器工具箱。打开的窗口看起来像Firefox DevTools,但实际上您正在调试整个浏览器。现在快速进入网络选项卡,如果您在20秒之前完成了所有这些操作,则会看到您的网络请求已经过并且能够检查它。

祝你好运!