原生js掉接口
1.get请求
var inputpath = document.getElementById("inputpath").value; //获取输入框的值作为参数
// 创建一个ajax对象
const xhr = new XMLHttpRequest();
xhr.open("get", urls + '?url=' + inputpath, );
xhr.onload = function() {
if (xhr.status === 200) {
const data = JSON.parse(xhr.responseText);
console.log(data);
if (data.code == 200) {
// myvideo.style.display = "block";
video = data.data.url
}else{
// video=""
// myvideo.style.display = "none";
}
} else {
console.error("请求失败");
}
};
// 发送请求
xhr.send();
2.post请求
const xhr = new XMLHttpRequest();
const url = 'http://example.com/api';
const data = {
name: 'John',
age: 30
};
const params = JSON.stringify(data);
xhr.open('POST', url, true);
xhr.setRequestHeader('Content-type', 'application/json');
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
const response = JSON.parse(xhr.responseText);
console.log(response);
}
};
xhr.send(params);

浙公网安备 33010602011771号