Jquery从远程主机获取json [英] Jquery Get json from remote host

查看:263
本文介绍了Jquery从远程主机获取json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想通过使用这段代码从远程主机读取json。

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://50.116.19.49/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>

问题是当我在浏览器中输入url,但是没能通过使用上面的jquery方法得到json。

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

推荐答案

您有跨网域问题,因此您需要使用JSONP,因此请如下更改jquery方法

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


如果URL包含字符串callback =? (或类似的,由
服务器端API定义),请求被视为JSONP。

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://50.116.19.49/rest/user.json?jsoncallback=?",function(result){
  $.each(result, function(i, field){
    $("div").append(field + " ");
      });
    });

jQuery手册: http://api.jquery.com/jQuery.getJSON/

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

这篇关于Jquery从远程主机获取json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆