
Ajax
1.引入jQuery库
html

JS
2.编写Ajax请求的方法
Javascript
//发送Ajax请求并获取数据
functionAjaxRequest(url,data={},method='get'){
return$.Ajax({
url:url,
data:data,
method:method,
dataType:'JSon',
headers:{
'Content-Type':'application/JSon',
},
async:true,
});
}
3.调用Ajax请求的方法
Javascript
AjaxRequest('/api/data')
.done((response)=>{
console.log(response);
})
.fAIl((err)=>{
console.log(err);
});
4.后端同学需要配置响应头Access-Control-Allow-Origin,以允许跨域访问
以上就是Ajax的基本使用方式,需要根据具体的业务场景和后端接口做出适当的修改。
Copyright © 2025 IZhiDa.com All Rights Reserved.
知答 版权所有 粤ICP备2023042255号