Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
630 views
in Technique[技术] by (71.8m points)

react-native fetch跨域请求返回的Response.ok==fase,但控制台已经显示能够获取到数据

在用 fetch API 实现跨域请求时,遇到这个问题,跨域请求是成功的,但是返回的 response 中 body 为 null ,response.ok==false,
图片描述

在chrome浏览器可以看到,返回的数据已经有了,
图片描述

我使用是fetch获取百度音乐API的,我写的代码如下:

var url="http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.search.catalogSug&query=张碧晨"
fetch(url, {
    method: 'GET',
    mode: 'no-cors',
    cache: 'default'
}).then((res)=> res.json()).then((resData) => {
    console.log(resData);
})

这样打印出来的,显示有错误,不知道是什么问题?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

no-cors 的请求回来的结果 opaque response,是一种不透明 的响应结果,意味着你不能通过 response 获取。应该采用 cors 跨域请求模式,可以正常拿到请求结果,但是这种模式涉及到CORS,需要服务端支持 Access-Control-Allow-Origin 响应头。具体可参考MDN


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...