使用jQuery写前端接口

使用 $.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 = [];
                }
            });

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值