【问题标题】:jsonp jquery cross domain errorjsonp jquery跨域错误
【发布时间】:2014-12-17 12:22:07
【问题描述】:

我正在尝试使用 jQuery 和 jsnop 检索 IMDB 页面的源代码,使用以下代码:

$(document).ready(function (){

        var url = "http://www.imdb.com";

        var success = function(data){
            // work
        };

        $.ajax({
          type: 'GET',   
          url: url,
          data:{todo:"jsonp"},
          dataType: "jsonp",
          crossDomain: true,         
          cache:false,
          success: success,
          error:function(jqXHR, textStatus, errorThrown){
            console.log(errorThrown);
            console.log(textStatus);
          }
        });
});

我收到以下错误:

object error
parse error

【问题讨论】:

标签: javascript jquery ajax json jsonp


【解决方案1】:

实际上问题在于格式,因为您正在点击http://www.imdb.com 它将返回基本上是 xml 格式的 html,但是您的 ajax 调用需要 JSON 格式,请尝试 yahoo ypl

//喜欢这个

var site = 'http://www.imdb.com';
var encoderUrl = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent('select * from xml where url="' + site + '"') + '&format=xml&callback=?';
$.getJSON(encoderUrl, function(data){
    console.log(data);
});

【讨论】:

    猜你喜欢
    • 2013-04-08
    • 2013-04-04
    • 1970-01-01
    • 2013-01-04
    • 2013-10-23
    • 2012-09-08
    • 1970-01-01
    • 2013-05-10
    • 2011-06-22
    相关资源
    最近更新 更多