我是reactjs新手: 我有一个带有一项服务的 springboot,它返回一个像这样的 ResponseEntity:
@RequestMapping(value = "/startTest")
public ResponseEntity<String> runTest {
String response;
if (this.values>0){
response=String.format("Test Failed: %s", summary.getFailures().get(0).getException().getMessage());
}
else{
response="Test Successfull!";
}
return ResponseEntity.status(HttpStatus.OK).body(response);
在 react 中,我有这个读取正文的函数:
var url = "http://127.22.22.12:8090/startTest";
await fetch(url)
.then(function(response){return response.json();})
.then(function(data){const items=data;
console.log(items)
})
但我在控制台中出现此错误:
Uncaught (in promise) SyntaxError: Unexpected token T in JSON at position 0
如果写 console.log(response.text()) 我查看 Promise OBJECT 但如何获取文本形式的 PROMISE VALUE?
谢谢 问候
最佳答案
您正在使用async await在你的代码中。你不需要连环 promise 。 试试这个
var url = "http://127.22.22.12:8090/startTest";
const result = await fetch(url)
console.log(result.text())
此外,您的 fetch 函数应该是一个 async
函数,以便使用 async wait。
关于javascript - 如何从 React 中的 get 服务(Springboot)读取 Response Promise 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60776051/