Getting JSONP through jQuery

UPDATE 1:

This is what I get in the browser if I type

http://www.remote_host.com/feed.php?callback=jsonpCallBack

{ "rss": { "channels": [ { "title": "title goes here", "link": "http://www.remote_server.com/feed.php", "description": "description goes here", "items": [ { "title": "item title goes here", "link": "item link goes here", "pubDate": "item date goes here", "description": "item description goes here" }, { "title": "item title goes here", "link": "item link goes here", "pubDate": "item date goes here", "description": "item description goes here" }, { "title": "item title goes here", "link": "item link goes here", "pubDate": "item date goes here", "description": "item description goes here" } ] } ] } } 

So this is not jsonp?

ORIGINAL QUESTION:

I have the following script where I am trying to get json data from a remote host:

 $(document).ready(function() { get_json_feed(); function get_json_feed() { $.ajax({ url: 'http://www.remote_host.com/feed.php?type=json', type: 'GET', dataType: 'jsonp', error: function(xhr, status, error) { alert("error"); }, success: function(json) { alert("success"); } }); } }); 

But for some reason, I get an error and warning:

Note: the resource is interpreted as a script, but is transmitted with the MIME type text / HTML.

Error: does not throw SyntaxError: Unexpected token:

What am I doing wrong?

+6
source share
3 answers

JSONP "relies on a site that meets your request using a JavaScript form expression,

  someFunction( someJSON ) 

The name of the function is provided as an argument from your code, and the idea is that the script response, once consumed and interpreted by the browser, will cause this function to be called using a parsed JSON blog - that is, a JavaScript object. The jQuery library will do some accounting work for you, even if you create a global scope for the call (which will be the code that just calls the callback that you provide as the success argument).

Thus, you should check what the actual response from this server looks like. It seems to me that this is not a server ready to respond in this way. You may need to specify an additional parameter in your url of the form "callback =?".

+7
source

I don’t know exactly what error you encountered, but there are useful tips for using jsonp here

  • error : this handler is not called for cross-domain script and JSONP requests.
  • write jsonp: 'callback', jsonpCallback: 'jsonpCallback' in ajax options. Setting jsonp for the callback and then setting jsonpCallback to jsonpCallback makes the request like this:

    http://domain.com/jsonp-demo.php?callback=jsonpCallback&name=watever

  • Loads into a JSON block using JSONP. Add an extra ?callback=? at the end of your url to indicate a callback.

Your complete script would look like this:

 <script> $(document).ready(function(){ $("#useJSONP").click(function(){ $.ajax({ url: 'http://domain.com/jsonp-demo.php', data: {name: 'Chad'}, dataType: 'jsonp', jsonp: 'callback', jsonpCallback: 'jsonpCallback', success: function(){ alert("success"); } }); }); }); function jsonpCallback(data){ $('#jsonpResult').text(data.message); } </script> 

Example here

+6
source

It looks like the server is returning an invalid Content-type header.

+3
source

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


All Articles