weixin_33720186 2016-12-20 17:14 采纳率: 0%
浏览 63

Ajax请求上为什么出错误了?

我在Ajax请求上遇到了麻烦,我得到了以下错误:

Error: Access is denied

我尝试了这个jQueryAjax请求:

$.support.cors = true;
$.ajax({
    url: 'http://testwebsite.com/test',
    type: "GET",
    dataType: 'json',
    contentType: 'application/json',
    crossDomain: true,
    headers: {
        'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
        'Access-Control-Allow-Origin': 'http://testwebsite/test',
        'Access-Control-Allow-Headers': '*',
        'Access-Control-Allow-Credentials': 'true'
    },
    xhrFields: {
        withCredentials: true
    },
    success: function(data) {
        alert("Data from Server" + JSON.stringify(data));
    },
    error: function(jqXHR, textStatus, errorThrown) {
        alert("You can not send Cross Domain AJAX requests: " + errorThrown);
    }
});

谁能告诉我哪里出错了?提前谢谢你。

  • 写回答

1条回答 默认 最新

  • ℡Wang Yan 2016-12-20 17:24
    关注

    As rory-mccrossan mentioned in a comment, CORS protection is designed so that a website cannot directly access the content of another website (in a browser) unless the website being accessed agrees to it.

    It would completely ruin the point of CORS if a site just has to send some headers in order to circumvent the defence

    Instead, you will need to use a CORS proxy. It's a server that when given a request like yourCORSproxy.example.com/http://testwebsite/test would load the page and return it, but with CORS allowed (like a normal proxy but with CORS enabled).

    One of these CORS proxies is https://crossorigin.me, but at the time of writing this it is down. It might be more stable if you simply create your own proxy.


    tl;dr: testsite.test should be sending the headers on its response. You should not be sending the headers on your request.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog