从localhost使用谷歌联系人api创建联系人时出现CORS错误

CORS error on creating contact with google contacts api from localhost

本文关键字:联系人 错误 CORS 创建 api localhost 谷歌      更新时间:2023-09-26

我在请求时收到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方法中我得到了错误。

很容易,您应该首先设置serverhttp响应头。这与您的前端javascript代码无关。您需要返回此标题:

Access-Control-Allow-Origin:*

Access-Control-Allow-Origin:your domain