springboot怎么把数据传给前端:springboot 如何引入前端 bootstrap?
这种问题网上一搜一大堆,你可以具体找一篇文章试试,遇到问题可以针对相关问题去提问。springboot通过jar包方式引入bootstrap_个人文章 - SegmentFault 思否 这不是查查就
顺晟科技
2022-10-19 11:34:56
123
我试图使用fetch api带回一些数据,但是在检索到数据后无法将其映射到控制台。
fetch('http://jsonplaceholder.typicode.com/users', {
method: 'GET'
}).then(function(response) {
console.log(response)
response.forEach(i => console.log(i.name));
}).catch(function(err) {
console.log(`Error: ${err}` )
});
我得到的错误是
response.map不是函数
所以我试图解析响应(即var data=json.parse),但没有工作,出现错误
fetch('http://jsonplaceholder.typicode.com/users', {
method: 'GET'
}).then(function(response) {
console.log(response)
response.forEach(i => console.log(i.name));
}).catch(function(err) {
console.log(`Error: ${err}` )
});
有趣的是,在对XMLHttp请求执行相同的操作时,我需要解析它,因此我也想知道这两种检索数据的方法之间的差异。
如果有人能给我指明正确的方向,我将不胜感激。
顺晟科技:
理解承诺是使用获取API的关键。
当您试图解析响应并遍历它时,响应实际上只是一个承诺。为了利用来自请求的实际响应的内容,您必须执行一些承诺链接。
fetch('http://jsonplaceholder.typicode.com/users', {
method: 'GET'
}).then(function(response) {
console.log(response)
response.forEach(i => console.log(i.name));
}).catch(function(err) {
console.log(`Error: ${err}` )
});
获取API在承诺中返回响应流。响应流不是JSON,因此尝试调用它将失败。要正确解析JSON响应,需要使用response.JSON函数。这将返回一个承诺,以便您可以继续链。
fetch('http://jsonplaceholder.typicode.com/users', {
method: 'GET'
}).then(function(response) {
console.log(response)
response.forEach(i => console.log(i.name));
}).catch(function(err) {
console.log(`Error: ${err}` )
});
您可能不正确地访问了json。你可以试着打个电话。
fetch('http://jsonplaceholder.typicode.com/users', {
method: 'GET'
}).then(function(response) {
console.log(response)
response.forEach(i => console.log(i.name));
}).catch(function(err) {
console.log(`Error: ${err}` )
});
此示例的结构与您的示例相匹配,但理想情况下,您将返回第一个块,然后继续下一个块。这里是一个类似的示例,将继续下一个块。
在您的特殊情况下,您可以将Fetch API作为“XMLHttpRequest”的json感知包装器来查看。主要区别在于Fetch API更简单,类似于函数,并且有方便的方法。David Walsh在他的博客文章中进行了合理的比较,我建议您看一下。普通的“XMLHttpRequest”只是传递从服务器返回的字符串,它不知道它可能是JSON,因此让用户以他们认为合适的方式解析响应。
05
2022-12
02
2022-12
02
2022-12
29
2022-11
29
2022-11
24
2022-11