简体   繁体   English

来自附加SDK的AJAX Put请求

[英]AJAX Put request from Add-on SDK

For the last 3 hours, I've been working on making a put request on a Firefox add-on from the main.js / index.js page with no luck. 在过去的3个小时中,我一直在努力地从main.js / index.js页面上对Firefox附加组件提出放置请求,但没有成功。

From documentation, this is the correct AJAX to call (with the correct site, if you wanted to try to edit it) 从文档中,这是正确的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 Add-on's SDK doesn't use $.ajax() , but instead uses Request() . Firefox附加组件的SDK不使用$.ajax() ,而是使用Request() So, I've made the code as so: 因此,我已经编写了如下代码:

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();

But whatever I do, it just does not work. 但是,无论我做什么,都行不通。 The response comes out with "constructor{}) 响应带有“构造函数{}”

I can do a get request to the site just fine. 我可以向网站提出获取请求。

Sadly the request object doesn't let you handle errors so when things go wrong you're just left wondering. 遗憾的是,请求对象不允许您处理错误,因此当出现问题时,您只会感到疑惑。 You could try using the net/xhr module if you want more control. 如果需要更多控制,可以尝试使用net/xhr模块。

I'm not sure what the issue is here but it seems like the server doesn't like the object it is seeing. 我不确定这里是什么问题,但是服务器似乎不喜欢它所看到的对象。

I found this error message was returned (as a JSON object): 我发现返回了此错误消息(作为JSON对象):

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

More importantly here's a quick hack to help you debug things yourself. 更重要的是,这里有一个快速的技巧可以帮助您自己调试。

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);

This code gives wraps your request in a 20 second delay timer. 此代码将您的请求包装在20秒的延迟计时器中。 That is 20 seconds from the time Firefox starts up and runs this add-on code, so not a lot of time but just enough to complete this task. 距离Firefox启动并运行此附加代码的时间仅20秒,因此花费的时间不是很多,而足以完成此任务。

Once Firefox has started up via your jpm run command. Firefox通过jpm run命令启动后。 Immediately go to the Tools menu. 立即转到“工具”菜单。 Open Tools -> Web Developer -> Browser Toolbox. 打开工具-> Web开发人员->浏览器工具箱。 The window opened will look like the Firefox DevTools but you're actually debugging the whole browser. 打开的窗口看起来像Firefox DevTools,但实际上是在调试整个浏览器。 Now quickly go to the network tab and if you did all this before 20 seconds expired you'll see your network request go through and be able to inspect it. 现在,快速进入“网络”标签,如果您在20秒过期之前进行了所有操作,您将看到您的网络请求通过并可以对其进行检查。

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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