h_h*_*h_h 5 javascript jquery cross-domain
嗨,我试图通过使用这段代码从远程主机读取json.
<!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>
Run Code Online (Sandbox Code Playgroud)
问题是当我在浏览器中键入url时,我从中获取json.但是使用上面的jquery方法无法获得json.
有人可以帮助这方面.谢谢
您有跨域问题,因此您需要使用 JSONP,因此更改您的 jquery 方法如下
如果 URL 包含字符串“callback=?” (或类似的,由服务器端 API 定义),请求将被视为 JSONP。
$.getJSON("http://xx.xxx.xxx.xx/rest/user.json?jsoncallback=?",function(result){
$.each(result, function(i, field){
$("div").append(field + " ");
});
});
Run Code Online (Sandbox Code Playgroud)
jQuery 手册: http: //api.jquery.com/jQuery.getJSON/
| 归档时间: |
|
| 查看次数: |
30560 次 |
| 最近记录: |