我是使用XMLHttpRequest的新手,我想我似乎遗漏了一些明显的东西-我有一个非常有效的URL,试图从中检索JSON数据,但是当我调用send on onerror函数时,但是onload没有(即使我得到了200)。您可以在此处查看测试页:https://pwas.resourcedata.com/walkipedia/webrequest.html
function MakeWebRequest() {
url = "https://data.wsdot.wa.gov/arcgis/rest/services/Shared/TownshipSection/MapServer/identify?geometry=-13606913.506613478%2C5937651.515662&geometryType=esriGeometryPoint&sr=&layers=all&layerDefs=&time=&layerTimeOptions=&tolerance=1&mapExtent=-13609094.35057674%2C5936620.810108784%2C-13602038.257402979%2C5939181.450556404&imageDisplay=400%2C300%2C96&returnGeometry=true&maxAllowableOffset=&geometryPrecision=&dynamicLayers=&returnZ=false&returnM=false&gdbVersion=&f=pjson";
let xhr = new XMLHttpRequest();
xhr.onload = function () {
debugger;
let data = JSON.parse(xhr.responseText);
}
xhr.onerror = function (x ) {
alert("Error while calling Web API");
}
let httpVerb = "GET";
xhr.open(httpVerb, url);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.send();
}
答案 0 :(得分:1)
删除此行:
learnerRF = makeLearner("regr.ranger", par.vals = list("num.trees" = 5000))
parsRF = makeParamSet(
makeIntegerParam("mtry", lower = 0 , upper = 10),
)
tuneRF = makeTuneControlGrid()
inner = makeResampleDesc("CV", iters = 10)
learnerRF = makeTuneWrapper(learnerRF, resampling = inner, par.set = parsRF,control = tuneRF, show.info = FALSE)
这是没有必要的,因为xhr.setRequestHeader("Content-Type", "application/json");
请求没有任何内容(JSON在响应中)。并且触发了CORS验证错误。
如果您想指定只需要JSON响应,请使用:
GET
答案 1 :(得分:1)
删除此行:
xhr.setRequestHeader("Content-Type", "application/json");
请检查此小提琴的输出: https://jsfiddle.net/2e9jz6x3/2/