我是 Web 開發的新手,我在 JavaScript 中使用 XMLHttpRequest() 從 api 獲取資料。我正在嘗試從資料中創建變數,但是當我嘗試執行以下操作時出現錯誤。有誰知道這條線有什么問題var data1 = data["data1"];?
<script>
const Http = new XMLHttpRequest();
const url = "www.mytestapi.com/response.json";
Http.open("GET", url);
Http.send();
Http.onreadystatechange = (e) => {
var json = JSON.parse(Http.responseText)
var data = json.Data
var data1 = data["data1"]; //issue caused here
}
<script>
uj5u.com熱心網友回復:
您不需要決議回應資料,資料已經決議,試試這個
const xhr = new XMLHttpRequest();
const url = "www.mytestapi.com/response.json";
xhr.open('GET', url);
xhr.responseType = 'json';
xhr.onload = () => {
console.log("load - " JSON.stringify(xhr.response));
var data = xhr.response;
var data1 = data["data1"]
}
xhr.onerror = () => {
console.log("error status - " xhr.status);
}
xhr.send()
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/433499.html
標籤:javascript html css json 网络
上一篇:vs代碼在引號中包含多行
