在Vanilla JS中发送AJAX post请求并接收回JSON数据的方法如下:
var xhr = new XMLHttpRequest();
xhr.open("POST", "请求的URL", true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
var response = JSON.parse(xhr.responseText);
// 处理返回的JSON数据
}
};
var data = {
key1: value1,
key2: value2
};
xhr.send(JSON.stringify(data));
完整的示例代码如下:
var xhr = new XMLHttpRequest();
xhr.open("POST", "请求的URL", true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
var response = JSON.parse(xhr.responseText);
// 处理返回的JSON数据
}
};
var data = {
key1: value1,
key2: value2
};
xhr.send(JSON.stringify(data));
这种方法可以在Vanilla JS中发送AJAX post请求并接收回JSON数据。在实际应用中,可以根据具体的业务需求进行相应的参数配置和数据处理。
没有搜到相关的沙龙
领取专属 10元无门槛券
手把手带您无忧上云