从ajax post()返回值的方法有多种,以下是其中几种常见的方式:
$.post(url, data, function(response) {
// 处理返回的数据
console.log(response);
});
function postData(url, data) {
return new Promise(function(resolve, reject) {
$.post(url, data, function(response) {
resolve(response);
}).fail(function(error) {
reject(error);
});
});
}
// 调用方式
postData(url, data)
.then(function(response) {
// 处理返回的数据
console.log(response);
})
.catch(function(error) {
// 处理错误
console.error(error);
});
async function postData(url, data) {
try {
const response = await $.post(url, data);
// 处理返回的数据
console.log(response);
} catch (error) {
// 处理错误
console.error(error);
}
}
// 调用方式
postData(url, data);
以上是几种常见的从ajax post()返回值的方法,根据具体情况选择适合的方式进行处理。
领取专属 10元无门槛券
手把手带您无忧上云