JavaScript中以json格式提交data发起post请求的代码
可以使用jQuery库中的$.ajax方法来发起post请求,示例代码如下:
$.ajax({
url: 'yourUrl',
type: 'POST',
dataType: 'json',
data: {
key1: value1,
key2: value2,
key3: value3
},
success: function(data) {
// 请求成功回调函数
},
error: function(xhr, textStatus, error) {
// 请求失败回调函数
}
});
其中,url为请求的地址,type为请求方法,dataType为预期返回的数据类型,data为要提交的数据,success和error分别为请求成功和失败的回调函数。需要注意的是,如果要以json格式提交数据,需要将data参数设置为一个json对象。
原文地址: https://www.cveoy.top/t/topic/0fA 著作权归作者所有。请勿转载和采集!