layui怎么实现ajax请求数据
- 引入jQuery或者layui自带的jquery模块,例如:
layui.use(['jquery'], function(){
var $ = layui.jquery;
// ajax请求代码
});
- 使用$.ajax()方法发送请求,例如:
$.ajax({
url: 'http://example.com/data', // 请求的url地址
type: 'GET', // 请求方式
data: { // 请求参数
username: 'admin',
password: '123456'
},
success: function(data){ // 请求成功回调函数
console.log(data);
},
error: function(xhr, textStatus, errorThrown){ // 请求失败回调函数
console.log(xhr.status);
console.log(textStatus);
console.log(errorThrown);
}
});
其中,url是请求的地址,type是请求的方式,data是请求的参数,success是请求成功后的回调函数,error是请求失败后的回调函数。在回调函数中可以对返回的数据进行处理和展示。
注意:在使用layui的时候,需要在layui.use()中引入jquery模块才能使用$符号,例如:
layui.use(['jquery'], function(){
var $ = layui.jquery;
$.ajax({ ... });
});
原文地址: https://www.cveoy.top/t/topic/bE7S 著作权归作者所有。请勿转载和采集!