6

Hi i am trying to read json from a remote host by using this piece of code.

<!DOCTYPE html>
<html>
<head>
<script src="jquery.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$.getJSON("http://xx.xxx.xxx.xx/rest/user.json",function(result){
  $.each(result, function(i, field){
    $("div").append(field + " ");
      });
    });
  });
});
</script>
</head>
<body>

<button>Get JSON data</button>
<div></div>

</body>
</html>

The problem is when i type the url in browser i get json from it. But failed to get json by using the above jquery method.

Can someone help in this regard. Thanks

5
  • 2
    Unless the remote host adds the appropriate cross-origin headers, you can't. Are you in control of the remote service? Commented Oct 20, 2012 at 16:43
  • The above given json service is Drupal rest service. Can you tell me how should i do cross origin headers. Thankyou. Commented Oct 20, 2012 at 16:45
  • does REST have jsonp output version, if so use that Commented Oct 20, 2012 at 16:47
  • The rest service retuns an array Commented Oct 20, 2012 at 16:55
  • i have try the code in IE and i am getting [object Object] [object Object] can any body help me in parsing the json Commented Oct 20, 2012 at 17:09

3 Answers 3

6

I am gong to assume this page is not being served from the site that hosts the JSON.

You are attempting to make a cross-domain request, which most(?) browsers to do allow. You are encountering what is called the same-origin policy of the browser. It is a security measure built into the browser. It will not allow you to make an XHR request to a location that is not on the same origin as the requesting page.

There are a few ways around this:

  1. Use a server-side proxy to make the request
  2. use JSONP to make the request (See GBD's answer)
  3. Look into CORS
Sign up to request clarification or add additional context in comments.

4 Comments

Hi i have checked the last link and also verified that my site is already CORS enabled.
Then look at the JS Console, what is it telling you? What browser are you using?
I am using Chrome. Is there any alternate way of doing this any jquery or javascript hack.
@h_a86 u may check your console if its giving cross domain access violation you may try proxy server if you are using php you may try curl for this purpose to fetch the json from cross domain and then pass it to your jquery thanks.
4

You have cross-domain issue, so you need to use JSONP so change your jquery method as below

If the URL includes the string "callback=?" (or similar, as defined by the server-side API), the request is treated as JSONP instead.

$.getJSON("http://xx.xxx.xxx.xx/rest/user.json?jsoncallback=?",function(result){
  $.each(result, function(i, field){
    $("div").append(field + " ");
      });
    });

jQuery Manual: http://api.jquery.com/jQuery.getJSON/

2 Comments

i have checked this but its not working. My site is CORS enabled.
what are you getting alert(field ) ?
0

Cross domain queries aro not allowed. See ajax jquery: can't get json/xml from other domain for possible workaround. Other way is use proxy on same domain to proxy local queries to remote hosts.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.