使用 $.ajax请求参数:paged=1 Body请求参数(raw-json){ "scene_id": "4" //<写死>项目id }post接口http://localhost/lpro/service/page/node 怎么写?
我们可以直接请求
$.ajax({
url: 'http://localhost/lpro/service/page/node', // 请求 URL(不需要包含 query 参数,因为 query 参数会在 `data` 里传递)
type: 'POST', // 请求方法
dataType: 'json', // 服务器响应的数据格式(JSON)
contentType: 'application/json', // 设置请求头,告诉服务器请求体是 JSON 格式
data: JSON.stringify({ // 请求体数据,转化为 JSON 字符串
scene_id: "4" // 写死的项目 ID
}),
success: function(response) {
console.log('请求成功:', response); // 处理成功响应
},
error: function(xhr, status, error) {
console.log('请求失败:', error); // 处理请求错误
}
});
还可封装函数:
/**
* 请求接口函数封装
**/
function commonAjax(method,url, data,sucessCallBack, errorCallBack) {
var config = {
type : method ,
url : url,
contentType : "application/json",
dataType : "json",
async : true,
success : function(data){
if(data.status == 20000){
return;
}else{
sucessCallBack(data);
}
},
error : errorCallBack
};
if(typeof(ucode) != "undefined"){
config.headers={ 'USER-KEY' :ucode };
}
if(method.toUpperCase() != 'GET' && method.toUpperCase() != 'DELETE' ){
config.data = JSON.stringify(data);
}
$.ajax(config)
};
// 判断函数返回值是否正确函数
var isOk= isOK ;
function isOK(e){
if(e.status == 2 ){
return true ;
}else{
return false ;
}
}
函数调用
commonAjax('POST', baseurl + '/service/page/node?paged=1', {
scene_id: "4"
}, function (e) {
if (isOK(e)) {
let data = e.data.data
data.sort((x,y)=>{
return x.sort - y.sort
})
that.pagesNodeObj = data;
} else {
that.pagesNodeObj = [];
}
});