Is it possible to use javascript to load a JSON object from another domain / server?

What does this code look like?

+3
source share
4 answers

Other domains / servers need to support JSONP, which basically wraps JSON in the callback.

In jQuery, the call would look like this:

$.getJSON(
     'http://otherdomain.com/api/whatever?callback=?', 
     { key: 'value', otherkey: true },
     function(data){
        //handle response
     }
);

The actual response from another server (if you looked at what is actually being sent) would look like this:

// With this url:
http://domain.com/api/method?callback=the_callback_function_name

// The response would look like this:
the_callback_function_name({ "json": "data here"});

jQuery getJSON JSONP callback=?. , , json_callback=?. , URL- callback: '?' data getJSON.

+5

JSONP. , jQuery - , script :

<script type="text/javascript" src="http://path.to/your/javascript"></script>

<script> . , JSONP. script JSON :

{a: 0, b: 1}

, . JSONP , -

<script type="text/javascript" src="http://path.to/your/javascript?callback=yourCallbackFunction"></script>

:

yourCallbackFunction({a: 0, b: 1});

, script.

+4
+1

. asp.net/php, , ajax -.

0

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


All Articles