我正在呼叫以下后端字串:
"hello there"
作為前端,我使用的是 Vue
<script>
export default {
name: "test",
data() {
return {
r:'',
msg: '',
};
},
methods: {
loadMsg() {
},
},
mounted () {
var requestOptions = {
method: 'GET',
redirect: 'follow'
};
fetch("http://localhost:8080/api/v1/msgs/1", requestOptions)
.then(response => response.json())
.then(result => result.forEach(msg=> {
this.msg.push(msg)
}) )
.catch(error => console.log('error', error));
}
};
</script>
但我收到以下錯誤:
error SyntaxError: Unexpected token h in JSON at position 0
但是,當我使用 console.log() 時,我會按預期在控制臺上得到按摩。
我想要的只是將字串傳遞給data()tothis.msg
那么有什么想法可以解決這個問題嗎?
uj5u.com熱心網友回復:
您只能呼叫.json()JSON 回應,顯然,您的回應只是純文本
還有一個問題,你msg是一個string(你將它初始化為''),但它.push()是一個陣列方法
uj5u.com熱心網友回復:
考慮到內容hello there沒有引號,回應不是有效的 JSON,這會導致決議回應時出錯。
response.text()應該使用而不是response.json().
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/417274.html
標籤:
