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 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持