[jQuery] Re: cross domain getJSON nothing happens
the json file named myjson.json (I've reduced its contents to its minimum for testing purposes and it validates in JSONLint): {result: true} The javascript: $.getJSON('http://site1:/myjson.json', {}, function(data) { alert (data.result); }) I'm testing it in a local server (MAMP), and the code above works as expected. The problem is when I place the query that contains the callback (? format=jsoncallback=?), as I understand correctly, this is the way to get cross domain json: $.getJSON('http://site1:/myjson.json?format=jsoncallback=?', {}, function(data) { alert(data.result); }) It doesn't show the alert, either when in my local server or in an outside server. By looking at the firebug console/net panel, I can see that the response + json are correct, but the alert simply doesn't happen... All this because of the cross domain query. I've tried all the other ways ($.ajax(), $.get(), $.post()) and after adding the callback query nothing happens! I've even tryed adding the ( ) and doing an eval, but nothing... Anything I put inside the function(data) {} is ignored. Both of these methods work fine for me: $.getJSON('http://site1:/myjson.json?format=jsoncallback=?', function(data) { alert(data.result); }); $.ajax({ url: 'http://site1:/myjson.json?format=json', dataType: 'jsonp', success: function(data) { alert(data.result); } });
[jQuery] Re: cross domain getJSON nothing happens
thanks for the reply, but it gave me an invalid label with my json, which as I've read before, can be solved by eval'ing it with the parenthesis: var myObj = eval( ( + someJsonString + ) ); but after eval'ing, nothing happens (again), no alert :( In firebug I can see the json is there... Both of these methods work fine for me: $.getJSON('http://site1:/myjson.json?format=jsoncallback=?', function(data) { alert(data.result); }); $.ajax({ url: 'http://site1:/myjson.json?format=json', dataType: 'jsonp', success: function(data) { alert(data.result); } });
[jQuery] Re: cross domain getJSON nothing happens
See if this reply i made yesterday helps you out at all http://groups.google.com/group/jquery-en/browse_thread/thread/1525b2d017246957?hl=en# On Oct 23, 9:23 am, jayQuery xaudio...@yahoo.co.uk wrote: thanks for the reply, but it gave me an invalid label with my json, which as I've read before, can be solved by eval'ing it with the parenthesis: var myObj = eval( ( + someJsonString + ) ); but after eval'ing, nothing happens (again), no alert :( In firebug I can see the json is there... Both of these methods work fine for me: $.getJSON('http://site1:/myjson.json?format=jsoncallback=?', function(data) { alert(data.result); }); $.ajax({ url: 'http://site1:/myjson.json?format=json', dataType: 'jsonp', success: function(data) { alert(data.result); } });
[jQuery] Re: cross domain getJSON nothing happens
didn't help... the strange thing is that if I don't use jsonp (by using the callback query), it works locally. But obviously won't work when in a cross domain environment. On Oct 23, 2:37 pm, MorningZ morni...@gmail.com wrote: See if this reply i made yesterday helps you out at all http://groups.google.com/group/jquery-en/browse_thread/thread/1525b2d...
[jQuery] Re: cross domain getJSON nothing happens
the strange thing is that if I don't use jsonp (by using the callback query), it works locally. But obviously won't work when in a cross domain environment. It's not as easy as putting callback=? in the url.. you have to wrap the JSON you are generating in a function name so that jQuery can process it when it gets the response... cross domain jsonp absolutely for sure works, but you are not implementing it correctly... maybe show more code and what Firebug shows the response to be? On Oct 23, 9:50 am, jayQuery xaudio...@yahoo.co.uk wrote: didn't help... the strange thing is that if I don't use jsonp (by using the callback query), it works locally. But obviously won't work when in a cross domain environment. On Oct 23, 2:37 pm, MorningZ morni...@gmail.com wrote: See if this reply i made yesterday helps you out at all http://groups.google.com/group/jquery-en/browse_thread/thread/1525b2d...
[jQuery] Re: cross domain getJSON nothing happens
I see what you mean... I thought it was that simple... So I should generate my json via a server-language (like php) and echo that callback as in the post you refered? I'll try that and keep you posted. On Oct 23, 3:22 pm, MorningZ morni...@gmail.com wrote: the strange thing is that if I don't use jsonp (by using the callback query), it works locally. But obviously won't work when in a cross domain environment. It's not as easy as putting callback=? in the url.. you have to wrap the JSON you are generating in a function name so that jQuery can process it when it gets the response... cross domain jsonp absolutely for sure works, but you are not implementing it correctly... maybe show more code and what Firebug shows the response to be? On Oct 23, 9:50 am, jayQuery xaudio...@yahoo.co.uk wrote: didn't help... the strange thing is that if I don't use jsonp (by using the callback query), it works locally. But obviously won't work when in a cross domain environment. On Oct 23, 2:37 pm, MorningZ morni...@gmail.com wrote: See if this reply i made yesterday helps you out at all http://groups.google.com/group/jquery-en/browse_thread/thread/1525b2d...
[jQuery] Re: cross domain getJSON nothing happens
I thought it was that simple... So I should generate my json via a server-language (like php) and echo that callback as in the post you refered? I'll try that and keep you posted. Yes. Something like: ?php header('Content-type: application/json'); $cb = $_GET[callback]; echo $cb . ({ 'result': 'success' }); ?
[jQuery] Re: cross domain getJSON nothing happens
I'm a .NET guy and don't know the syntax for PHP, but maybe I could put it in plain terms - variable CallBackName = value of callback (which jQuery will replace ? with jsonpNNN where NN is a timestamp just to be random - variable MyResults is an object that is expressed in a JSON string (i know that it's easy to write/convert-to JSON in php but I don't know the specifics) - If there is a CallBackName value, then write out CallBackName( my JSON result ) otherwise, just write out my JSON result So in html if i had $.getJSON(local_call.php, ..) i'd return from the server { foo: 1, bar: 2} but if i had $.getJSON(http://remoteserver/local_call.php?callback=?;, ..) jQuery would replace ? with something like jsonp2345654345, so my code would return jsonp2345654345( { foo: 1, bar: 2} ) the example on the jQuery docs under .getJSON (using Flickr) is a great thing to read and the article @ IBM that is inside my previous link is an excellent overview as well. once you understand how it works, that makes it easy :-) On Oct 23, 10:27 am, jayQuery xaudio...@yahoo.co.uk wrote: I see what you mean... I thought it was that simple... So I should generate my json via a server-language (like php) and echo that callback as in the post you refered? I'll try that and keep you posted. On Oct 23, 3:22 pm, MorningZ morni...@gmail.com wrote: the strange thing is that if I don't use jsonp (by using the callback query), it works locally. But obviously won't work when in a cross domain environment. It's not as easy as putting callback=? in the url.. you have to wrap the JSON you are generating in a function name so that jQuery can process it when it gets the response... cross domain jsonp absolutely for sure works, but you are not implementing it correctly... maybe show more code and what Firebug shows the response to be? On Oct 23, 9:50 am, jayQuery xaudio...@yahoo.co.uk wrote: didn't help... the strange thing is that if I don't use jsonp (by using the callback query), it works locally. But obviously won't work when in a cross domain environment. On Oct 23, 2:37 pm, MorningZ morni...@gmail.com wrote: See if this reply i made yesterday helps you out at all http://groups.google.com/group/jquery-en/browse_thread/thread/1525b2d...
[jQuery] Re: cross domain getJSON nothing happens
nice one! this was the missing thing in my logic, I thought it was just a simple call to a file, but I know now that that file has to be generated according to the query. thanks to all the replies!
[jQuery] Re: cross domain getJSON nothing happens
Hi, I was perusing the group looking to help a colleague with a similar problem. One thing I noticed here, and maybe this has no bearing at all, was the difference between the syntax in the first two posts: jayQuery wrote: $.getJSON('http://site1:/myjson.json?format=jsoncallback=?', {}, function(data) { alert(data.result); }) Mike Alsup wrote: $.getJSON('http://site1:/myjson.json?format=jsoncallback=?', function(data) { alert(data.result); }); Those are different from one another. jayQuery passes an empty set of braces as second argument to getJSON, whereas Mike's passes the data function as the 2nd argument. Also, jay's overall wrapper function is not closed with a semicolon. Not sure if it has any bearing at all, just noting that one says his works and the other says his doesn't, but they are different. On Oct 23, 8:47 am, Mike Alsup mal...@gmail.com wrote: the json file named myjson.json (I've reduced its contents to its minimum for testing purposes and it validates in JSONLint): {result: true} The javascript: $.getJSON('http://site1:/myjson.json', {}, function(data) { alert (data.result); }) I'm testing it in a local server (MAMP), and the code above works as expected. The problem is when I place the query that contains the callback (? format=jsoncallback=?), as I understand correctly, this is the way to get cross domain json: $.getJSON('http://site1:/myjson.json?format=jsoncallback=?', {}, function(data) { alert(data.result); }) It doesn't show the alert, either when in my local server or in an outside server. By looking at the firebug console/net panel, I can see that the response + json are correct, but the alert simply doesn't happen... All this because of the cross domain query. I've tried all the other ways ($.ajax(), $.get(), $.post()) and after adding the callback query nothing happens! I've even tryed adding the ( ) and doing an eval, but nothing... Anything I put inside the function(data) {} is ignored. Both of these methods work fine for me: $.getJSON('http://site1:/myjson.json?format=jsoncallback=?', function(data) { alert(data.result); }); $.ajax({ url: 'http://site1:/myjson.json?format=json', dataType: 'jsonp', success: function(data) { alert(data.result); } });
[jQuery] Re: cross domain getJSON nothing happens
and maybe this has no bearing at all it has zero bearing. in the code (which $.getJSON calls): get: function( url, data, callback, type ) { // shift arguments if data argument was ommited if ( jQuery.isFunction( data ) ) { callback = data; data = null; } On Oct 23, 11:02 am, Chris ccshan...@gmail.com wrote: Hi, I was perusing the group looking to help a colleague with a similar problem. One thing I noticed here, and maybe this has no bearing at all, was the difference between the syntax in the first two posts: jayQuery wrote: $.getJSON('http://site1:/myjson.json?format=jsoncallback=?', {}, function(data) { alert(data.result); }) Mike Alsup wrote: $.getJSON('http://site1:/myjson.json?format=jsoncallback=?', function(data) { alert(data.result); }); Those are different from one another. jayQuery passes an empty set of braces as second argument to getJSON, whereas Mike's passes the data function as the 2nd argument. Also, jay's overall wrapper function is not closed with a semicolon. Not sure if it has any bearing at all, just noting that one says his works and the other says his doesn't, but they are different. On Oct 23, 8:47 am, Mike Alsup mal...@gmail.com wrote: the json file named myjson.json (I've reduced its contents to its minimum for testing purposes and it validates in JSONLint): {result: true} The javascript: $.getJSON('http://site1:/myjson.json', {}, function(data) { alert (data.result); }) I'm testing it in a local server (MAMP), and the code above works as expected. The problem is when I place the query that contains the callback (? format=jsoncallback=?), as I understand correctly, this is the way to get cross domain json: $.getJSON('http://site1:/myjson.json?format=jsoncallback=?', {}, function(data) { alert(data.result); }) It doesn't show the alert, either when in my local server or in an outside server. By looking at the firebug console/net panel, I can see that the response + json are correct, but the alert simply doesn't happen... All this because of the cross domain query. I've tried all the other ways ($.ajax(), $.get(), $.post()) and after adding the callback query nothing happens! I've even tryed adding the ( ) and doing an eval, but nothing... Anything I put inside the function(data) {} is ignored. Both of these methods work fine for me: $.getJSON('http://site1:/myjson.json?format=jsoncallback=?', function(data) { alert(data.result); }); $.ajax({ url: 'http://site1:/myjson.json?format=json', dataType: 'jsonp', success: function(data) { alert(data.result); } });
[jQuery] Re: cross domain getJSON nothing happens
My code was ok, the missing semicolon was because of the copy/paste. The problem was due to a faulty logic on my side and calling a static json. After these good people gave me more insight, I understood that the way to achieve a cross domain call to a json, the json needs to be built dynamically, writing the callback. After I did that, I got it to work correctly. On Oct 23, 4:02 pm, Chris ccshan...@gmail.com wrote: Hi, I was perusing the group looking to help a colleague with a similar problem. One thing I noticed here, and maybe this has no bearing at all, was the difference between the syntax in the first two posts: jayQuery wrote: $.getJSON('http://site1:/myjson.json?format=jsoncallback=?', {}, function(data) { alert(data.result); }) Mike Alsup wrote: $.getJSON('http://site1:/myjson.json?format=jsoncallback=?', function(data) { alert(data.result); }); Those are different from one another. jayQuery passes an empty set of braces as second argument to getJSON, whereas Mike's passes the data function as the 2nd argument. Also, jay's overall wrapper function is not closed with a semicolon. Not sure if it has any bearing at all, just noting that one says his works and the other says his doesn't, but they are different. On Oct 23, 8:47 am, Mike Alsup mal...@gmail.com wrote: the json file named myjson.json (I've reduced its contents to its minimum for testing purposes and it validates in JSONLint): {result: true} The javascript: $.getJSON('http://site1:/myjson.json', {}, function(data) { alert (data.result); }) I'm testing it in a local server (MAMP), and the code above works as expected. The problem is when I place the query that contains the callback (? format=jsoncallback=?), as I understand correctly, this is the way to get cross domain json: $.getJSON('http://site1:/myjson.json?format=jsoncallback=?', {}, function(data) { alert(data.result); }) It doesn't show the alert, either when in my local server or in an outside server. By looking at the firebug console/net panel, I can see that the response + json are correct, but the alert simply doesn't happen... All this because of the cross domain query. I've tried all the other ways ($.ajax(), $.get(), $.post()) and after adding the callback query nothing happens! I've even tryed adding the ( ) and doing an eval, but nothing... Anything I put inside the function(data) {} is ignored. Both of these methods work fine for me: $.getJSON('http://site1:/myjson.json?format=jsoncallback=?', function(data) { alert(data.result); }); $.ajax({ url: 'http://site1:/myjson.json?format=json', dataType: 'jsonp', success: function(data) { alert(data.result); } });