【问题标题】:Cross origin error in ajax callajax调用中的跨源错误
【发布时间】:2014-05-22 12:38:47
【问题描述】:
function getInfo()
{

    var URL="https://api-oauth2.mendeley.com/oauth/authorize?client_id=374&redirect_uri=https://localhost/api/mendeley/mendeley.php&response_type=code&scope=all&JSON=1";
    $.ajax({
        url: URL,
        type: 'get',
        dataType: "jsonp",
        jsonpCallback: "https://192.168.2.210/api/mendeley/mendeley.php",
        converters: {
            'text json': true
        },
        success: function (data) {
            console.log(data.content);


        },
        error: function(e) {
            console.log(e);
        }
    });
}

当我调用这个函数时,这给了我错误
SyntaxError:语法错误

【问题讨论】:

  • 您在哪一行得到错误?是syntax error 还是cross origin error
  • 如果我把警报放在错误中:function(e){} 它会给我那个警报

标签: ajax cross-domain mendeley


【解决方案1】:

你有几个问题。


来自jQuery documentation

jsonpCallback
类型:String 或 Function()
指定回调 JSONP 请求的函数名。将使用此值代替 jQuery 自动生成的随机名称。最好是 让 jQuery 生成一个唯一的名称,这样更易​​于管理 请求并提供回调和错误处理。你可能想要 当您想要启用更好的浏览器缓存时指定回调 GET 请求。从 jQuery 1.5 开始,您还可以为此使用函数 设置,在这种情况下 jsonpCallback 的值设置为返回 该函数的值。

https://192.168.2.210/api/mendeley/mendeley.php 不是有效的 JavaScript 函数名。


如果我访问 https://api-oauth2.mendeley.com/oauth/authorize?client_id=374&redirect_uri=https://localhost/api/mendeley/mendeley.php&response_type=code&scope=all&JSON=1https://api-oauth2.mendeley.com/oauth/authorize?client_id=374&redirect_uri=https://localhost/api/mendeley/mendeley.php&response_type=code&scope=all&JSON=1&callback=something,我会收到一个 HTML 文档,提示我登录。

您需要点击一个真正的 JSONP 端点才能使用 JSONP。

【讨论】:

  • 谢谢你的回答。我在我的 ajax 代码 jsonpCallback: "result", jsonp: 'callback' 和 function result(data) { alert(data);控制台.log(数据); };
  • @sayali — 这个答案指出了两个问题。你提到了其中一位。它不起作用也就不足为奇了。
猜你喜欢
  • 2021-06-03
  • 2016-08-24
  • 2015-04-05
  • 1970-01-01
  • 2015-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多