从localhost创建与谷歌联系人API的联系时出现CORS错误

时间:2015-12-15 09:05:43

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

使用以下代码请求时出现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方法中我收到错误。

1 个答案:

答案 0 :(得分:-1)

很简单,您应首先设置服务器 http响应标题。与您的前端JavaScript代码无关。您需要返回此标题:

Access-Control-Allow-Origin:*

Access-Control-Allow-Origin:your domain