var xhr = new XMLHttpRequest(); xhr.open('POST', 'save_table.php'); xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); xhr.send('data=' + encodeURIComponent(data));
时间: 2023-12-30 15:05:40 浏览: 42
这段代码是使用XMLHttpRequest对象向服务器发送POST请求并传递数据的示例。下面是代码的详细解释:
1. `var xhr = new XMLHttpRequest();`:创建XMLHttpRequest对象的实例,该对象用于发送HTTP请求和接收HTTP响应。
2. `xhr.open('POST', 'save_table.php');`:初始化HTTP请求参数。在这里,我们使用POST方法向名为save_table.php的服务器端脚本发送请求。
3. `xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');`:设置HTTP请求头。在这里,我们设置请求的内容类型为application/x-www-form-urlencoded,表示我们将发送表单数据。
4. `xhr.send('data=' + encodeURIComponent(data));`:发送HTTP请求。在这里,我们将data参数的值编码为URL编码格式,并将其作为请求的主体发送到服务器端脚本。这里的data参数表示要传递给服务器端脚本的数据。
总的来说,这段代码用于向服务器端脚本发送POST请求并传递数据,可用于通过AJAX技术更新网页的内容。
相关问题
function fn2() { let xhr = new XMLHttpRequest() xhr.open('post', '/api/users/login') xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded') xhr.send('uname=小妲己&password=gx123456') xhr.onreadystatechange = function () { if (xhr.readyState === 4 && xhr.status === 200) { console.log(JSON.parse(xhr.responseText)); } } }这段代码中open里面的地址为啥不用加端口号
&pwd=123456') xhr.onreadystatechange = function() { if(xhr.readyState === 4 && xhr.status === 200) { console.log(xhr.responseText) } } } 这段代码的作用是什么?
这段代码的作用是向后端发送一个POST请求,登录用户。具体来说,代码中通过XMLHttpRequest发送了一个POST请求到指定路径'/api/users/login',并且将用户名和密码以'application/x-www-form-urlencoded'的格式放在请求体中发送到后端。当后端响应成功(状态码为200)时,将返回内容打印到控制台中。
封装ajax function ajaxGet(url, callback) { var xhr = new XMLHttpRequest(); xhr.open('GET', url, true); xhr.send(); xhr.onreadystatechange = function () { if (xhr.readyState == 4 && xhr.status == 200) { callback(xhr.responseText); } } } // 调用 ajaxGet('/user/?ac=sdkIsGray', function (data) { var data = JSON.parse(data); if(data.data.sdk_is_gray===1){ localStorage.setItem('sdk_is_gray','1') var classVal = document.getElementById("sdk_body").getAttribute("class"); classVal = classVal.replace("colorful","gray"); document.getElementById("sdk_body").setAttribute("class",classVal ); }else{ localStorage.setItem('sdk_is_gray','0') var classVal = document.getElementById("sdk_body").getAttribute("class"); classVal = classVal.replace("gray","colorful"); document.getElementById("sdk_body").setAttribute("class",classVal ); } });
好的,这里是封装了 `ajax` 的代码:
```javascript
function ajax(method, url, data, callback) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
callback(null, xhr.responseText);
} else {
callback(new Error('请求失败'));
}
}
};
xhr.open(method, url, true);
if (method == 'POST') {
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(JSON.stringify(data));
} else {
xhr.send();
}
}
```
这个函数接受四个参数:
- `method`:HTTP 请求的方法,可以是 `'GET'` 或 `'POST'`。
- `url`:请求的 URL。
- `data`:POST 请求的数据,应该是一个对象。
- `callback`:请求完成后的回调函数,接受两个参数,第一个参数是错误对象,第二个参数是响应文本。
使用这个函数,你可以像这样发起一个 GET 请求:
```javascript
ajax('GET', '/user/?ac=sdkIsGray', null, function(err, data) {
if (err) {
console.error(err);
return;
}
var data = JSON.parse(data);
if (data.data.sdk_is_gray === 1) {
localStorage.setItem('sdk_is_gray', '1');
var classVal = document.getElementById('sdk_body').getAttribute('class');
classVal = classVal.replace('colorful', 'gray');
document.getElementById('sdk_body').setAttribute('class', classVal);
} else {
localStorage.setItem('sdk_is_gray', '0');
var classVal = document.getElementById('sdk_body').getAttribute('class');
classVal = classVal.replace('gray', 'colorful');
document.getElementById('sdk_body').setAttribute('class', classVal);
}
});
```
或者像这样发起一个 POST 请求:
```javascript
var formData = {
name: 'John',
age: 20,
};
ajax('POST', '/user/', formData, function(err, data) {
if (err) {
console.error(err);
return;
}
console.log('POST 请求成功');
});
```
希望这可以帮到你!