0

There are several questions on Stack Overflow saying cross domain AJAX request etc will not work because of security reasons. Some questions like this explain this. (Please let me know if I am wrong.)

This is working perfectly fine:

$(document).ready(function() { 
  $.getJSON("http://search.twitter.com/search.json?q=test&callback=?", function(data) {
    alert("test alert outside loop");
    $.each(data.results, function() {
      alert("test alert inside loop");
    });
  });
});

But just replacing the URL with my application won't work. In that case response code is 200, but there's no response data. There is an hit on my application; I can see that in console.

$(document).ready(function() { 
  $.getJSON("http://192.168.1.2:3000/cities.json?callback=?", function(data) {
    alert("test alert outside loop");
    $.each(data.results, function() {
      alert("test alert inside loop");
    });
  });
});

I am developing a very simple mobile app using PhoneGap so I need to make this call using JavaScript. But the main thing that's confusing me is why the Twitter call is working, but the call to my app isn't. I've also tried to remove the protect_from_forgery call in my application controller in my Rails app, but i don't think that matters.

EDIT

i have deployed the app on http://deals.textadda.com/cities.json now check it... Its not working..

U can try it. these two links http://jsfiddle.net/2arbY/ http://jsfiddle.net/fHxf9/

Community
  • 1
  • 1
Mohit Jain
  • 43,139
  • 57
  • 169
  • 274
  • Where are you calling these functions from? A server, or your local machine? Is `http://192.168.1.2` the IP address of the machine running the script? – Alex Oct 03 '11 at 09:21
  • @Alex i have deployed the app on http://deals.textadda.com/cities.json now check it... Its not working.. – Mohit Jain Oct 03 '11 at 11:19
  • [This seems related](http://stackoverflow.com/questions/6591987/json-received-but-error-for-resource-interpreted-as-script-but-transferred-with/6592013#6592013). Looks like you're returning JSON from the server, not JSONP. – Alex Oct 03 '11 at 12:45

3 Answers3

1

Probably data.results doesn't exist, even if data does. What do you get if you alert(data); (or console.log(data); ) outside the loop?

EDIT

Your app isn't generating a callback wrapper. For instance, http://deals.textadda.com/cities.json?callback=abc should generate a JSON object wrapped in a function call of abc, in the same way, for example, the twitter response does: http://search.twitter.com/search.json?q=test&callback=abc.

graphicdivine
  • 10,937
  • 7
  • 33
  • 59
  • Ah. What happens, than, if you put the bare url(ie http://192.168.1.2/cities.json) into a browser. what's the response? – graphicdivine Oct 03 '11 at 09:23
  • take a look on this.. it returns a json response http://cl.ly/2W123L1j3G1x0e1n2R1x – Mohit Jain Oct 03 '11 at 10:23
  • @grapphicdivine i have deployed the app on http://deals.textadda.com/cities.json now check it... Its not working.. – Mohit Jain Oct 03 '11 at 11:18
  • I am not getting you. Can you explain what should i do.. L – Mohit Jain Oct 03 '11 at 11:54
  • Well, if the JSON response isn't generating a callback, possibly just a call to `cities.json` (without the `?callback=?` modifier) is enough. – graphicdivine Oct 03 '11 at 12:02
  • Dude... even thats not workin.... U can try it. these two links http://jsfiddle.net/2arbY/ http://jsfiddle.net/fHxf9/ – Mohit Jain Oct 03 '11 at 12:12
  • Well, accessing the json from jsfiddle isn't going to work because of same-origin-policy. If you need to access from the JSON from a different domain to where it is served from, then you need to fix the callback wrapper. – graphicdivine Oct 03 '11 at 12:21
1

you are running into cross-domain issues due to same-origin-policy the ip you are trying to get the json from should reside on the same sever from which you are originating the request.

try using

 $.getJSON("192.168.1.2/cities.json?callback=?", func
Rafay
  • 30,950
  • 5
  • 68
  • 101
1

The problem is that this remote server returns JSON, not JSONP. It returns:

{"lines":[{"line":"COLOMBO - BADULLA"},{"line":"COLOMBO - MATALE"},{"line":"COLOMBO - PUTTLAM"},{"line":"COLOMBO - THANDIKULAM"},{"line":"COLOMBO - TALAIMANNAR"},{"line":"COLOMBO - BATTICALOA"},{"line":"COLOMBO - TRINCOMALEE"},{"line":"COLOMBO - MATARA"},{"line":"COLOMBO - AVISSAWELLA"},{"line":"COLOMBO - MIHINTALE"}]}

instead of:

someCallbackName({"lines":[{"line":"COLOMBO - BADULLA"},{"line":"COLOMBO - MATALE"},{"line":"COLOMBO - PUTTLAM"},{"line":"COLOMBO - THANDIKULAM"},{"line":"COLOMBO - TALAIMANNAR"},{"line":"COLOMBO - BATTICALOA"},{"line":"COLOMBO - TRINCOMALEE"},{"line":"COLOMBO - MATARA"},{"line":"COLOMBO - AVISSAWELLA"},{"line":"COLOMBO - MIHINTALE"}]})

Thats why I was not be able to consume a remote domain using AJAX unless this remote resource supports JSONP.

Mohit Jain
  • 43,139
  • 57
  • 169
  • 274