Jsonp callback error

I am using firefox addon builder. The execution of these "callback undefined" code errors

function callback(data) { window.alert(data.status); } $.ajax({ url: "http://apps.compete.com/sites/google.com/trended/rank/?apikey=210e634a0b3af972daa908a447c735c1&start_date=201112&end_date=201112&jsonp=?", dataType: "jsonp", jsonp: "jsonp", jsonpCallback: "callback" }); 

This is the api documentation: https://www.compete.com/developer/documentation/

+4
source share
4 answers

I assume that you are using this from the contents of the script. You should keep in mind that content scripts do not really execute in the same context as web page scripts - a web page cannot see the functions defined by content scripts and vice versa (a detailed description of this mechanism ). JSONP works by inserting a <script> into a web page. This script will run in the context of the web page - and it will not see the callback function that you define in the script content.

To define the callback function in a window context, do the following:

 unsafeWindow.callback = function(data) { window.alert(data.status); }; 

However, you should take the warnings about unsafeWindow in the documentation seriously and avoid them if possible. To download data, use request package in your extension:

 require("request").Request({ url: "http://apps.compete.com/sites/google.com/trended/rank/?apikey=210e634a0b3af972daa908a447c735c1&start_date=201112&end_date=201112", onComplete: function(response) { console.log(response.json); } }); 

You can then send response.json to your content script through regular messaging .

+3
source

Try it.

 $.ajax({ url: "http://apps.compete.com/sites/google.com/trended/rank/?apikey=210e634a0b3af972daa908a447c735c1&start_date=201112&end_date=201112&jsonp=?", dataType: "jsonp", success: function(data) { window.alert(data.status); } }); 
0
source

You should not add jsonp=? to your url, this is done using the ajax function.

Use only:

 url: "http://apps.compete.com/sites/google.com/trended/rank/?apikey=<your-api-key>&start_date=201112&end_date=201112", 
0
source

Actually, in response to Marcelo Diniz, and anyone trying to get a competing API works:
Do you need &jsonp=? add to your url, otherwise your ajax request will always fail.

I struggled with this for a while, because the documents from the competitions were vague.

0
source

Source: https://habr.com/ru/post/1392321/


All Articles