【问题标题】:Getting error while calling a remote web API调用远程 Web API 时出错
【发布时间】:2017-12-14 20:57:27
【问题描述】:

我正在使用网站并访问第三方 api 但出现错误 No 'Access-Control-Allow-Origin' header is present on the requested resource

阿贾克斯:

var headers = new Headers();
    headers.append('Access-Control-Allow-Origin', '*');
    headers.append('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, PUT');
    headers.append('Accept', 'application/json');
    headers.append('content-type', 'application/json');
    headers.append('AuthToken', '2948f47085e9d8ecd95bd21ebe024a01516105f9')
    headers.append('Access-Control-Allow-Origin', 'http://localhost:61030/Test.aspx');
    headers.append('Access-Control-Allow-Credentials', 'true');

    $.ajax({
        crossDomain: true,
        type: "GET",
        url: "https://api.travelcloudpro.eu/v1/cache/flyfrom?origin=DEL&pointOfSale=US",
        //headers: { '[{"key":"AuthToken","value":"2948f47085e9d8ecd95bd21ebe024a01516105f9","description":""}]': 'some value' },
        //  header: { 'AuthToken': '2948f47085e9d8ecd95bd21ebe024a01516105f9' },
        header:headers,
        dataType: "json",
        data:{},
        success: function (data) {
            debugger;
            var flightresponse = data;

            //alert($("#city").val());
        }
    });

虽然从邮递员那里调用它工作正常。

我不知道缺少什么,TIA。

如果我将数据类型 json 更改为 jsonp,则会出现此错误:- enter image description here

【问题讨论】:

标签: javascript json ajax asp.net-web-api


【解决方案1】:

我猜当您提到“第三方 api”时,您正在向具有不同 IP 地址(或端口号)的服务器发送请求,这在大多数浏览器(如 firefox 和 chrome)中会产生 CORS 错误(跨源资源共享)。

+here you can read more about why it happens.+

您需要添加一个在 +here+ 讨论的 CORS 过滤器。

我不是 .net 程序员,但我希望它可以提供帮助。

【讨论】:

  • 我没有得到你,我需要修改我的 ajax 调用吗?
  • 不,它必须从服务器端修复。 im sorry im 不熟悉 ASP.net 所以请参考我为您链接的页面。
猜你喜欢
  • 2015-01-30
  • 1970-01-01
  • 2014-12-25
  • 1970-01-01
  • 1970-01-01
  • 2015-12-08
  • 2018-05-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多