How to make jsonp call for api using jquery

I am new to programming in general and am having problems getting data in my web application when I call the moviedb.org api. I use jquery and I read all the documents, even the cookbook, and I still try my best to do this work, I also checked my Google Dev Tools console and showed that the call was successful:

Status Code:200 OK [{"id":550,"name":"Fight Club","posters":[{"image":{"type":"poster","size":"original","height":1000,"width":675,"url":"http://hwcdn.themoviedb.org/posters/f8e/4b........ 

Here is my code:

 <script> $.getJSON("http://api.themoviedb.org/2.1/Movie.getImages/en/json/ed7e2e092ca896037ce13d2bf11a08f1/550&callback=?", function(data){ $.each(data, function(i,item){ $("<img/>").attr("src", item.image).appendTo("#images"); }); }); </script> 

I think I was fiddling with the callback function, any thoughts would be appreciated. Thanks in advance!

+4
source share
3 answers

In short, the server you click on does not seem to support JSONP, this is server-side functionality that needs to be present ... and no. I also don't see mention in their docs or API forums. URL should be ?callback=? , since it is the only request parameter:

 http://api.themoviedb.org/2.1/Movie.getImages/en/json/<apikey>/550&callback=? 

... but it still won’t work, as the server does not support it. As well as an aside: many times something with an API key will not support JSONP (since the client will have a key) if the API using the key to restrict access ... others using it only for tracking may not worried.

+3
source

First of all, you need to make sure that your application api supports jsonp (if it is not, you will see json, but you cannot do anything with it).

Secondly. If you have not added a callback name to leave a message? Callback =? then jquery will add a random number at the end of your URL and will most likely cause a problem. It will look like this:

 http://someurl.com?callback=140989239 

It only hurts. In standard jsonp, you actually embed json in the <script> . To do this, you will want to do something like the following:

 var headID = document.getElementsByTagName('head')[0]; var newScript = document.createElement('script'); newScript.type = "text/javascript"; newScript.src = "enter your api url that you are calling here with a specified callback" headID.appendChild(newScript); 
+2
source

This post function calls the server

 $.post("http://api.themoviedb.org/2.1/Movie.getImages/en/json/ed7e2e092ca896037ce13d2bf11a08f1/550&callback=?", function(data){ //After the server code run, this code is executed with the //information of the response into the parameter 'data' $.each(data, function(i,item){ $("").attr("src", item.image).appendTo("#images"); }); }, json"); 

As you can see, I put the exact same β€œevery” iterative function. I do not know if you are doing this correctly because I am not executing this code. But I strongly recommend that you try to execute the code in the function (data) ... with a warning (...), as I will show below:

 $.post("http://api.themoviedb.org/2.1/Movie.getImages/en/json/ed7e2e092ca896037ce13d2bf11a08f1/550&callback=?", function(data){ alert(data); }, "json"); 

With this, you really know that s is "data" vale.

I hope you were helpful.

+1
source

All Articles