2016-02-04 37 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秒之前完成了所有這些操作,則會看到您的網絡請求已經過並且能夠檢查它。

祝你好運!