Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
101
rated 0 times [  105] [ 4]  / answers: 1 / hits: 26187  / 15 Years ago, wed, january 13, 2010, 12:00:00

Question:



I'm trying to use JSON accross domains, but all i find is JSON parsers, which I don't need...


I've read that it's possible to do cross-domain requests with JSON,
but so far, all I see is implementations that use XMLHttpRequest...

- which means you can't use cross-domain requests, at least not outside IE 8...


I've been on http://www.json.org/, but all I find is either parsers or useless.



The best I've found with google so far is

http://devpro.it/JSON/files/JSONRequest-js.html


but this is rather a mess, doesn't work cross domain, and intra-domain neither - or rather not at all...



var the_object = {}; 
var http_request = new XMLHttpRequest();
http_request.open( GET, url, true );
http_request.onreadystatechange = function () {
if ( http_request.readyState == 4 && http_request.status == 200 ) {
the_object = JSON.parse( http_request.responseText );
}
};
http_request.send(null);

More From » json

 Answers
4

What you can do cross-domain is inject a script include:



var s = document.createElement('script');
s.src = 'http://someotherdomain/getMeMyJs.aspx?parameter=value';
s.onload = someOptionalCallback;
s.type = 'text/javascript';

if(document.getElementsByTagName('head').length > 0)
document.getElementsByTagName('head')[0].appendChild(s);


Now, the code returned by that request will be executed immediately. If you want for that to interact with your code, you can make sure that it's being returned with all data wrapped in a function call:



jsonCallback({ object: json, whatever: value });


You can use that to build APIs, where you pass the name of a callback function as a request querystring parameter. Here's an example of such an API


[#97847] Sunday, January 10, 2010, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
patienceannel

Total Points: 674
Total Questions: 101
Total Answers: 101

Location: Northern Mariana Islands
Member since Fri, Jan 15, 2021
3 Years ago
patienceannel questions
Fri, Mar 11, 22, 00:00, 2 Years ago
Tue, Oct 20, 20, 00:00, 4 Years ago
Wed, Jul 24, 19, 00:00, 5 Years ago
;