ajax如何使用json數據格式化

在使用AJAX(Asynchronous JavaScript and XML)與服務器通信時,JSON(JavaScript Object Notation)是一種非...
在使用AJAX(Asynchronous JavaScript and XML)與服務器通信時,JSON(JavaScript Object Notation)是一種非常常見的數據交換格式。以下是如何使用JSON數據格式化的一些步驟:
1. 創建AJAX請求
你需要創建一個AJAX請求。你可以使用`XMLHttpRequest`對象或更現代的`fetch` API。
使用`XMLHttpRequest`:
```javascript
var xhr = new XMLHttpRequest();
xhr.open('GET', 'your-endpoint-url', true);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
var response = JSON.parse(xhr.responseText);
console.log(response);
本文由夕逆IT于2025-01-27發表在夕逆IT,如有疑問,請聯系我們。
本文鏈接:http://www.resource-tj.com/bian/359572.html
本文鏈接:http://www.resource-tj.com/bian/359572.html