|
本帖最后由 OK论坛 于 2024-9-3 01:33 编辑
1、get请求
- var xhr = new XMLHttpRequest();
- xhr.open('GET', 'https://www.oklun.com', true);
-
- xhr.onload = function () {
- if (xhr.status === 200) {
- console.log(xhr.responseText);
- } else {
- console.error('Request failed. Returned status of ' + xhr.status);
- }
- };
-
- xhr.onerror = function () {
- console.error('There was a network error!');
- };
-
- xhr.send();
复制代码
2、post请求
- var xhr = new XMLHttpRequest();
- xhr.open('POST', 'https://www.oklun.com', true);
-
- // 设置请求头,告诉服务器发送的内容类型
- xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
-
- xhr.onload = function () {
- if (xhr.status === 200) {
- console.log(xhr.responseText);
- } else {
- console.error('Request failed. Returned status of ' + xhr.status);
- }
- };
-
- xhr.onerror = function () {
- console.error('There was a network error!');
- };
-
- // 发送请求体数据
- xhr.send('name=John&age=30');
复制代码
|
|