在使用get方法時,如果獲取的數據是JSON格式的,需要做出一些特殊的處理,以下是具體步驟:
1. 發送GET請求獲取JSON數據 var request = new XMLHttpRequest(); request.open("GET", "url", true); request.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); request.onreadystatechange = function() { if (request.readyState === 4 && request.status === 200) { var responseJson = JSON.parse(request.responseText); //處理JSON數據 } }; request.send(); 2. 解析JSON數據 var responseJson = {"name":"Tom", "age":18}; console.log(responseJson.name); //輸出Tom console.log(responseJson["age"]); //輸出18 3. 遍歷JSON數據 var responseJson = {"name":"Tom", "age":18}; for (var key in responseJson) { console.log(key + ":" + responseJson[key]); //輸出name:Tom和age:18 }
上一篇python 計算高低點
下一篇mysql刪除數據庫錯誤