且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从jQuery中的外部URL获取数据?

更新时间:2023-02-23 19:48:12

这里有两件事:

首先,由于没有其他查询字符串,因此您的网址添加项应为"?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.