Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"(Callback method) is not defined" in JSONP access on Firefox Addon

I'm trying to get data from server using JSONP with jQuery's ajax method.

$.ajax({
    dataType: "jsonp",
    url: "https://secure.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json",
    type: "GET",
    data: "msg=aaa",
    cache: true,
    jsonp: "jsoncallback",
    // jsonpCallback: "callbackmethod",

    success: function(encryptedMsg){
        console.log("Encryption success!");
    },
    error: function(req, errmsg, thrownError) {
        console.log("Error: HTTP " + req.status + " " + errmsg);
    }
});

However, following error is shown in error console:

Error: jQuery1720502636097747291_1339479763752 is not defined
Source File: https://secure.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=jQuery1720502636097747291_1339479763752&msg=aaa
Line: 1

If you open the source file URL, you can see following JSON, and it seems correct JSON.

jQuery1720502636097747291_1339479763752({
    "title": "Recent Uploads tagged cat",
    // ...
    "items": [
    {
        "title": "Chaton",
        // ...
    },
    // ...
    ]
})

I also tried to specify method name with jsonpCallback: "callbackmethod", but it didn't work. I also used $.getJson() method and jquery-jsonp (http://code.google.com/p/jquery-jsonp/) but the result was the same.

The browser is Firefox and using HTML4. This is used in a firefox addon.

You can read full code here: https://builder.addons.mozilla.org/addon/1048275/revision/749
I use $.ajax in getEncryptedMessage function in common-content.js

Thanks in advance.

like image 817
Jumpei Ogawa Avatar asked Oct 23 '22 20:10

Jumpei Ogawa


1 Answers

Don't use JSONP in Firefox extensions. If it worked, it would be a security issue because it allowed some web server to run code in the context of your extension. In your case it doesn't work however because content scripts don't access the web page directly. The JSONP script gets to run in the context of the web page and cannot see functions defined by the content script.

You should use the request module to make requests to any web servers, it supports the JSON format. You cannot use it from a content script of course but you can send a message back to the extension, the extension should then make the request and send the server response back to the content script. See documentation on communicating with content scripts.

like image 152
Wladimir Palant Avatar answered Oct 27 '22 09:10

Wladimir Palant