【发布时间】:2018-02-01 17:52:58
【问题描述】:
我需要发送 XMLHttpRequest 到 Yandex 服务器
我收到错误请求错误 (400) 并且:
XMLHttpRequest 无法加载 http://api.lbs.yandex.net/geolocation。不 请求中存在“Access-Control-Allow-Origin”标头 资源。因此,Origin 'http://localhost' 不允许访问。 响应的 HTTP 状态代码为 400。
这是我的代码:
var xhr = new XMLHttpRequest();
var url = "http://api.lbs.yandex.net/geolocation";
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function () {
<some code>
};
var data = JSON.stringify({
"common": {
"version": "1.0",
"api_key": <here goes API key>
},
"gsm_cells": [
{
"countrycode": params[0],
"operatorid": params[1],
"cellid": params[3],
"lac": params[2],
"age": 0
}
]
});
xhr.send(data);
对于这么简单(?)的事情,我找不到解决方案!
【问题讨论】:
标签: javascript cors geolocation xmlhttprequest yandex