如何从 jQuery 中的外部 URL 获取数据? [英] How can I get data from an external URL in jQuery?

查看:39
本文介绍了如何从 jQuery 中的外部 URL 获取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑:

var resturl = "http://example.com";
cj.getJSON(
    resturl + "&callback=?",        
    function(data)
    {
       console.log(data);
    }
);

我的回调函数从未被调用.为什么?

My callback function is never called. Why?

推荐答案

这里有两点:

首先,您的 URL 添加应该是 "?callback=?" 因为没有其他查询字符串,或者使用完整的 $.ajax() 使用 jsonp 数据类型调用,因此它会根据需要添加查询字符串,像这样:

First, your URL addition should be "?callback=?" since there's no other querystring, or use the full $.ajax() call with a jsonp data type, so it adds the querystring as needed, like this:

$.ajax({
  url: resturl,
  dataType: 'jsonp',
  success: function(data){
    console.log( data );
  }
});

其次,您要访问的域必须支持 JSONP.

Second, the domain you're going to has to support JSONP.

这篇关于如何从 jQuery 中的外部 URL 获取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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