【ajax 05】$.ajax 与RESTful API

向服务器端传递函数名字的参数名称

客户端:src=“/js/jquery.min.js?callback

服务器:const callback = req.query.cb

​ const data = callback+"({name:‘zhangsan’})"

#jsonpCallback:‘callback

这样请求成功之后就会默认使用 callback 方法而不是 success()


$.get(‘urlAddress’,{name:‘zhangsan’,age:30},function(response){ })

$.post(‘urlAddress’,{name:‘zhangsan’,age:30},function(response){ })


    $('#btn').on('click',function(){ 
      // GET 方式请求
        $.get('/base','name=zhangsan&age=30',function(response){
            console.log(response)
        })// POST 方式请求  (无 Data)
      $.post('/base',function(response){
            console.log(response)
        })// POST 方式请求
      $.post('/base',{name:'zhangan',age:39},function(response){
            console.log(response)
        })});
【 jQuery 中 Ajax 全局事件 】

.ajaxStart( ) //请求开始时触发

.ajaxComplete( ) // 请求结束时触发

Ajax 全局事件一定要绑定在 document 上:

$(document).on('ajaxStart',function(){

​	console.log('start');

})
$(document).on('ajaxStart',function(){

​	console.log('start');

})

— 插件NProgress

<link rel='stylesheet' href='nprogress.css'/>
<script src='nprogress.js'></script>
NProgress.start();
NProgress.done();
RESTful API概述:

一套关于设计请求的规范:

方式动作
GET获取数据http://www.example.com/users
POST添加数据http://www.example.com/users
PUT更新ID为1的用户信息http://www.example.com/users/1
DELETE删除ID为1的用户信息http://www.example.com/users/1
GET获取用户ID为1的信息http://www.example.com/users/1
app.get('/user/:id',(req,res)=>{
    const id = req.params.id;
    res.send("当前我们是在获取id为${id}的用户信息");
});

app.delete('/user/:id',(req,res)=>{
    const id = req.params.id;
    res.send("当前我们是在删除id为${id}的用户信息");
});

app.put('/user/:id',(req,res)=>{
    const id = req.params.id;
    res.send("当前我们是在更新id为${id}的用户信息");
});

app.post('/user/:id',(req,res)=>{
    const id = req.params.id;
    res.send("当前我们是在添加id为${id}的用户信息");
});
<script>
	$.ajax({
    //type:'put',
    //type:'post',
    //type:'delete',
    type:'get',
    url:'/users/1',
    success:function(response){
      console.log(respomse)
    }
  })
</script>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值