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.

Community
  • 1
  • 1
Dawood Butt
  • 496
  • 6
  • 22
  • Have you tried using `$.get()` instead? It is quite often less problematic than `$.ajax()` - http://api.jquery.com/jQuery.get/ – Reinstate Monica Cellio Sep 18 '12 at 14:57
  • Could this be an issue with the trim() function? I know support for it is relatively new. Link: http://stackoverflow.com/questions/498970/how-do-i-trim-a-string-in-javascript – Steven Hunt Sep 18 '12 at 15:11
  • @Archer Thanks for your help, yes I tried $.get(), as well and the result is same. – Dawood Butt Sep 18 '12 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? – Reinstate Monica Cellio Sep 18 '12 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." – Dawood Butt Sep 18 '12 at 15:55
  • So, what is the context of this code? Are you trying to use it from main extension code or a content script? – Wladimir Palant Sep 18 '12 at 16:13
  • @WladimirPalant: I want to use this code in content script file named: `inject.js`. The following code I did in `main.js:` `var data = require("self").data;` `var pageMod = require("page-mod");` `pageMod.PageMod({ include: "*.com", contentScriptWhen: 'end', contentScriptFile: [ data.url("jquery.min.js"), data.url("inject.js") ] });` – Dawood Butt Sep 18 '12 at 17:09
  • @Dawood: Then it should work. The warning message you are quoting is unrelated. – Wladimir Palant Sep 18 '12 at 17:21
  • @WladimirPalant: YES! Exactly it should work. May be you are right that error coming on console due to some other code. But the point is: If I use simple jQuery code in `inject.js` like `$("#id").html('XXX');` it's working fine but the above code of `AJAX` which I mentioned in my question is not working at this place but working fine in other environment. Strange! The similar problem is also reported here: [.ajax is not working in firefox](http://forum.jquery.com/topic/ajax-is-not-working-in-firefox) – Dawood Butt Sep 18 '12 at 17:35

3 Answers3

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.

Charles Jourdan
  • 809
  • 7
  • 17
  • Thanks Charles, It seems to be my solution. let me try then I will let you know soon. – Dawood Butt Sep 19 '12 at 13:39
  • Wow did this help me answer a ton of questions with FF extensions. Thanks @Charles. – user516883 Apr 29 '13 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. – Hugo Gresse Oct 15 '19 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();
  }

}
Walty Yeung
  • 3,396
  • 32
  • 33
0

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

"permissions": [
    "tabs",
    "webRequest",
    "webRequestBlocking",
    "activeTab",
    "*://xxx/*"
],
Piotr Labunski
  • 1,638
  • 4
  • 19
  • 26