weixin_33721427 2015-09-11 19:50 采纳率: 0%
浏览 48

Chrome阻止了ajax请求[重复]

This question already has answers here:
                </div>
            </div>
                    <div class="grid--cell mb0 mt4">
                        <a href="/questions/20035101/why-doesn-t-postman-get-a-no-access-control-allow-origin-header-is-present-on" dir="ltr">Why doesn’t Postman get a “No 'Access-Control-Allow-Origin' header is present on the requested resource” error when my JavaScript code does?</a>
                            <span class="question-originals-answer-count">
                                (8 answers)
                            </span>
                    </div>
            <div class="grid--cell mb0 mt8">Closed <span title="2015-09-11 20:12:32Z" class="relativetime">4 years ago</span>.</div>
        </div>
    </aside>

All, I have the following ajax request that works fine in IE (I know right??) but when I try to use the same code in Chrome I get the following error:

XMLHttpRequest cannot load http://ahmwpsds01:1234/NASAService/GetNASAUser.asmx/GetUserByName. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://ahmwpsds01' is therefore not allowed access. The response had HTTP status code 500.

Ajax Request

function getNASAProfile(user) {
    var dfd = new $.Deferred(function () {
        $.support.cors = true;
        $.ajax({
            type: "POST",
            url: "http://ahmwpsds01:1234/NASAService/GetNASAUser.asmx/GetUserByName",
            data: "{userName: '" + UserProfile.Name + "'}",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            accept: "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
        })
        .done(function (data) {
            //alert('Call Succeeded');
            dfd.resolve(data.d);
        })
        .fail(function (jqXHR, textStatus, errorThrown) {
            alert('Call Failed

' + jqXHR.statusText + "

" + errorThrown + "

" + textStatus);
        });
    });
    return dfd.promise();
}

Any thoughts?

</div>
  • 写回答

1条回答 默认 最新

  • weixin_33688840 2015-09-11 20:01
    关注

    EDIT: Didn't notice that you want to make a POST request. You should consider using a backend method for your case

    You are trying to access a resource that is on a different server. For security reasons, you are not allowed to do that with a simple AJAX call.

    To work out the problem, you can use JSONP. The simple way to do it is to add ?callback=? to the URL. Plus, the server response must be wrapped in a function definition. You might want to use a proxy like YQL to convert the response to JSONP.

    More information on this link : http://json-jsonp-tutorial.craic.com/index.html

    评论

报告相同问题?

悬赏问题

  • ¥15 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)