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

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

问题描述

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

从不调用我的回调函数.有什么想法吗?

解决方案

这里有两件事:

首先,由于没有其他查询字符串,因此您的网址添加项应为"?callback=?",或使用完整的 数据类型为jsonp的调用,以便根据需要添加查询字符串,如下所示:

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

第二,您要使用的域必须支持 JSONP .

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

My callback function is never called. Any ideas?

解决方案

Two things here:

First your URL additon 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 );
  }
});

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

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

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