简体   繁体   中英

Addon firefox php request

i'm trying to develop Firefox extension

problem :

var Request = require("sdk/request").Request;
var latestTweetRequest = Request({
  url: "file.php",
  onComplete: function (response) {
    var List = response.json;
  }
});

I want to use this request function to parse json to an array (List here) from php file.

The php my php file echo json form correctly, but I can't transform the data into javascript array to be able to use it in my addon. if there is a better idea than using this function to do it please tell me :)

try this: MDN - JSON Object

JSON.parse and JSON.stringify

var Request = require("sdk/request").Request;
var latestTweetRequest = Request({
    url: "file.php",
    onComplete: function (response) {
        var List = JSON.parse(response.json);
    }
});

it's very important to use double quotes.

If you are having a problem with JSON.parse. Copy your array to scratchpad and then run JSON.stringify on it and then make sure your php file matches the strignified result.

if Addon-SDK doesnt have JSON then you gotta require the module if there is one. If there isn't one than require('chrome') and grab the component HERE

There's a bug in Noitidarts code.

why JSON.parse the request.json? If you want to parse do it on request.text

However no need to json.parse as the request module tries to parse and if successful retuns request.json

see here:

var Request = require("sdk/request").Request;
var latestTweetRequest = Request({
  url: "https://api.twitter.com/1/statuses/user_timeline.json?screen_name=mozhacks&count=1",
  onComplete: function (response) {
    var tweet = response.json[0];
    console.log("User: " + tweet.user.screen_name);
    console.log("Tweet: " + tweet.text);
  }
});

// Be a good consumer and check for rate limiting before doing more.
Request({
  url: "http://api.twitter.com/1/account/rate_limit_status.json",
  onComplete: function (response) {
    if (response.json.remaining_hits) {
      latestTweetRequest.get();
    } else {
      console.log("You have been rate limited!");
    }
  }
}).get();

so the likely problem is that your php is not outputting a json string that json.parse can read. make sure to use " . figure out what your php file should return by running json.stringify on a dummy object. ie:

var obj = {myarr:[1,8,9,7,89,0,'ji'],strr:'khhkjh',anothrtObj:{1:45,56:8}};
alert(JSON.stringify(obj)) //{"myarr":[1,8,9,7,89,0,"ji"],"strr":"khhkjh","anothrtObj":{"1":45,"56":8}}

so now in your php make sure your outputted text mateches this format

{"myarr":[1,8,9,7,89,0,"ji"],"strr":"khhkjh","anothrtObj":{"1":45,"56":8}}

if your php outputs something like below JSON.parse will fail on it so request.json will be null

{myarr:[1,8,9,7,89,0,"ji"],strr:"khhkjh",anothrtObj:{"1":45,"56":8}}

or

{'myarr':[1,8,9,7,89,0,"ji"],'strr':"khhkjh",'anothrtObj':{"1":45,"56":8}}

or

{'myarr':[1,8,9,7,89,0,'ji'],'strr':'khhkjh','anothrtObj':{'1':45,'56':8}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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