jquery ajax restful api 请求

后端用的是springboot

使用jquery封装了三种常用的请求方式

const appendUrl = function (suffix) {
    return "http://localhost:8011" + suffix;
};

//form get
const get = function (url, body, suc) {
    $.ajax({
        url: appendUrl(url),
        data: body,
        success: suc
    })
};

//json post
const jsonPost = function (url, body, suc) {
    $.ajax({
        url: appendUrl(url),
        type: "post",
        headers: {
        	"content-type":"application/json;charset=UTF-8"
        },
        data: JSON.stringify(body),
        success: suc
    })
};

//form post
const formPost = function (url, body, suc) {
    $.ajax({
        url: appendUrl(url),
        type: "post",
        data: body,
        success: suc
    })
};

get对应后端的@GetMapping
formPost对应后端的@PostMapping
jsonPost对应后端@PostMapping以及参数接收用了@RequestBody

调用方法

在这里插入图片描述
res就是后端返回的restful api返回体
{index: now}就是请求携带的body
如果不需要就放{}
这个url是被封装过拼接起来的 所以只需要除开前面相同部分的路径即可

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值