javascript - 使用jquery ajax跨域检索json数据

标签 javascript jquery ajax django cross-domain

我尝试使用 jquery ajax 从其他域检索 json 数据,但它不起作用。这是我的代码:

function getLeague() {
    $.ajax({
        url: 'http://otherdomainurl.ashx?username=xxx&pass=xxx&type=xxx',
        headers: { 'Access-Control-Allow-Origin': '*' },
        dataType: 'jsonp',
        async: false,
        crossDomain: true,
        success: function(data) {
            alert('Success');
        },
        error: function(error) {
            alert('Fail');
        }
    });
}

我尝试删除 header 、异步和跨域。我尝试将数据类型更改为 json。但它总是发出失败警报。我使用 django(但我认为这不是问题)。谢谢..

最佳答案

此 header 需要位于服务器端,而不是客户端。

尝试Django CORS Headers :

A Django App that adds CORS (Cross-Origin Resource Sharing) headers to responses.

Although JSON-P is useful, it is strictly limited to GET requests. CORS builds on top of XmlHttpRequest to allow developers to make cross-domain requests, similar to same-domain requests.

关于javascript - 使用jquery ajax跨域检索json数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35595088/

相关文章:

javascript - NG-显示令人恼火的图像位移

javascript - jQuery 未运行,没有错误消息。语法错误?

javascript - 检查对象参数是否未定义

javascript - 如何在后台对mysql数据进行匹配和分类?

php - ajax onclick 命令拉取过时的数据

javascript - 在 laravel 5.5 中通过 Javascript 获取总值(value)

java - 当我将字符串从 Javascript 传递到 Java applet 时,字符串会发生奇怪的变化

javascript - Polymer:无法在 <paper-button> 中使用 onclick 操作触发 jQuery 代码

javascript - Facebook Javascript API : tracking user adding application for first time

javascript - 使用 jQuery,如何禁用当前选项卡上的点击效果?