【问题标题】:CORS error on creating contact with google contacts api from localhost [duplicate]从本地主机创建与 google 联系人 api 的联系人时出现 CORS 错误 [重复]
【发布时间】:2015-12-15 09:05:42
【问题描述】:

我在使用以下代码请求时收到 Cross-Origin Request Blocked: 错误。

$.ajax({
    url: 'https://www.google.com/m8/feeds/contacts/default/full?access_token=' + token.access_token,
    type: 'POST',
    crossOrigin: true,
    crossDomain: true,
    Origin: 'http://localhost',
    Host: 'www.google.com',
    xhrFields: {
        withCredentials: true
        },
    dataType: 'xml',
    'Gdata-version': '3.0',
    'Content-Type': 'application/atom+xml',
    'body': xml,
    }).done(function(data) {
    console.log(data);
});

});

GET 方法以 jsonp(不适用于 POST 方法)作为数据类型检索联系人,没有任何错误,但在 POST 方法中出现错误。

【问题讨论】:

标签: javascript jquery xml google-api google-contacts-api


【解决方案1】:

很简单,你应该先设置服务器 http响应头。这与您的前端 javascript 代码无关。您需要返回此标头:

Access-Control-Allow-Origin:*

Access-Control-Allow-Origin:your domain

【讨论】:

  • google 向我发送响应标头,我该如何设置响应标头?
  • 请访问 ::web.requestly.in/# 创建自定义响应头
  • 仅供参考我的浏览器是firefox
猜你喜欢
  • 2023-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-25
相关资源
最近更新 更多