2

Here is my code:

$.ajax({
          url: 'xxx',
          success: function(data) 
          {
             if (data.trim() == '0')
             {
                //IF CODE
             } 
            else
            {
              //ELSE CODE
            }
         }
    });

This code working fine everywhere where I want to use, but not working in case of Firefox extension.

I read the following stackoverflow articles but no avail: Call to $.ajax from firefox extension not working and Ajax In Firefox Plugin

Also try to use xmlHTTPRequest but the result is same.

9
  • Have you tried using $.get() instead? It is quite often less problematic than $.ajax() - api.jquery.com/jQuery.get Commented Sep 18, 2012 at 14:57
  • Could this be an issue with the trim() function? I know support for it is relatively new. Link: stackoverflow.com/questions/498970/… Commented Sep 18, 2012 at 15:11
  • @Archer Thanks for your help, yes I tried $.get(), as well and the result is same. Commented Sep 18, 2012 at 15:34
  • Do you get any errors, or can you put some console.log() calls in so you can see where the code is running to? Commented Sep 18, 2012 at 15:46
  • @Archer Yes the error is coming on the console and that is: "The character encoding of the plain text document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the file needs to be declared in the transfer protocol or file needs to use a byte order mark as an encoding signature." Commented Sep 18, 2012 at 15:55

3 Answers 3

4

You should use the request module given by the addon sdk. You can call and use this module only in the add-on script and not in content script.

If you need to do an ajax request from a content script use the communication between content script and addon-script. You can find the documentation here.

If you want, I have an example of code (I assume it can be difficult to read it) but it can be help you.

3
  • Thanks Charles, It seems to be my solution. let me try then I will let you know soon. Commented Sep 19, 2012 at 13:39
  • Wow did this help me answer a ton of questions with FF extensions. Thanks @Charles.
    – user516883
    Commented Apr 29, 2013 at 19:37
  • Outdated! Support for extensions using XUL/XPCOM or the Add-on SDK was removed in Firefox 57, released November 2017. As there is no supported version of Firefox enabling these technologies, this page will be removed by December 2020. Commented Oct 15, 2019 at 19:12
1

Just in case it might help others of future, I would like to share a simple wrapper for the ajax Request. In most cases, the same jQuery Ajax configuration could be passed to function. (But it's not a complete conversion).

function sendAjax(ajaxConfig) {
  var request = Request({
    url: ajaxConfig.url,
    contentType: ajaxConfig.contentType,
    content: ajaxConfig.data,
    headers: ajaxConfig.headers,
    onComplete: function(response){
      var data = response.json;
      if(!data)
        data = response.text;

      //console.log("Ajax complete", response.status, ajaxConfig.url, response.text);    
      if(ajaxConfig.complete){
        ajaxConfig.complete(data);

      }

      if(response.status >= 400){ //got error
        if(ajaxConfig.error){
          //console.log("Ajax error", response.status, ajaxConfig.url, response.text);
          ajaxConfig.error(data);
        }
      }
      else{ //success
        if(ajaxConfig.success){
          //console.log("Ajax success", ajaxConfig.url, response.text);
          ajaxConfig.success(data);
        }
      }

    }

  });

  switch(ajaxConfig.type){
    case "POST":
      request.post();
      break;
    case "PUT":
      request.put();
    case "DELETE":
      request.delete();
    default:
      request.get();
  }

}
0

In manifest.json , You should add the url to the permissions : example:

"permissions": [
    "tabs",
    "webRequest",
    "webRequestBlocking",
    "activeTab",
    "*://xxx/*"
],
1
  • Please use the available code tags to make your answer more readable.
    – Paul
    Commented May 26, 2020 at 22:30

Not the answer you're looking for? Browse other questions tagged or ask your own question.