我有一个react/redux应用程序,我正在尝试对服务器执行简单的GET请求:
fetch('http://example.com/api/node', {
mode: "no-cors",
method: "GET",
headers: {
"Accept": "application/json"
}
}).then((response) => {
console.log(response.body); // null
return dispatch({
type: "GET_CALL",
response: response
});
})
.catch(error => { console.log('request failed', error); });
Run Code Online (Sandbox Code Playgroud)
问题是.then()函数中的响应体是空的,我不知道为什么.我在网上检查了一些例子,看起来我的代码应该可行,所以我显然在这里遗漏了一些东西.问题是,如果我检查Chrome的开发工具中的网络选项卡,则会发出请求并收到我正在寻找的数据.
任何人都可以对这一个发光吗?
编辑:
我尝试转换响应.
使用.text():
fetch('http://example.com/api/node', {
mode: "no-cors",
method: "GET",
headers: {
"Accept": "application/json"
}
})
.then(response => response.text())
.then((response) => {
console.log(response); // returns empty string
return dispatch({
type: "GET_CALL",
response: response
});
})
.catch(error => { console.log('request failed', error); }); …Run Code Online (Sandbox Code Playgroud) 当使用Fetch API从本地服务器上的REST API获取某些JSON并尝试解析响应时response.json(),我收到unexpected end of input错误.
使用以下单行程序可以在我的本地服务器上轻松重现该错误:
fetch(new Request('http://localhost:9950/RestService/v2/search/find/1', {mode: 'no-cors'})).then(function(response) { response.json(); });
// Output from Chrome console:
// Promise {[[PromiseStatus]]: "pending", [[PromiseValue]]: undefined}
// VM424:1 Uncaught (in promise) SyntaxError: Unexpected end of input
// at SyntaxError (native)
// at <anonymous>:1:128
Run Code Online (Sandbox Code Playgroud)
如果我查看网络选项卡,我可以看到我的请求是由服务器完成的,如果我预览响应,我可以看到那里有可用的有效JSON(我通过从网络复制和粘贴JSON来仔细检查这一点)响应选项卡到jsonlint.com).以下是我从服务器获得的响应示例:
{
"SearchMatches": [{
"Extracts": ["<b>1<\/b>.txt...", "<b>1<\/b>"],
"Title": "1.txt",
"Url": "C:\\TestShare4\\500Docs\\1.txt"
}]
}
Run Code Online (Sandbox Code Playgroud)
如果我尝试使用response.text()而不是response.json()我得到一个空字符串.
关于我可能在哪里出错的任何想法?
请注意,除了浏览器本身支持的内容之外,我还要求不依赖于任何其他框架或库.
我正在使用fetch API从其他API获取数据这是我的代码:
var result = fetch(ip, {
method: 'get',
}).then(response => response.json())
.then(data => {
country = data.country_name;
let lat = data.latitude;
let lon = data.longitude;
//fetch weather api with the user country
return fetch(`https://api.darksky.net/forecast/efc76f378da9a5bd8bb366a91ec6f550/${lat},${lon}`);
})
.then(response => response.json())
.then(data => {
// access the data of the weather api
console.log(JSON.stringify(data))
})
.catch(error => console.log('Request failed', error));
Run Code Online (Sandbox Code Playgroud)
但我在控制台中遇到错误:
Fetch API cannot load https://api.darksky.net/forecast/efc76f378da9a5bd8bb366a91ec6f550/52.3824,4.8995. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. If …Run Code Online (Sandbox Code Playgroud) 我正在尝试从localhost访问Deezer API,但我一直收到以下错误:
Fetch API cannot load http://api.deezer.com/search/track/autocomplete?limit=1&q=eminem.
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://localhost' is therefore not allowed access.
If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
Run Code Online (Sandbox Code Playgroud)
localhost的响应标头确实有Access-Control-Allow-Origin标头(Access-Control-Allow-Origin:*).
我使用的是像获取:
fetch('http://api.deezer.com/search/track/autocomplete?limit=1&q=eminem').
我究竟做错了什么?