Ajax - 在JQuery中发送Ajax请求

1. get/post请求
$.get(url,[data],[callback],[type]){
	url:请求的URL地址
	data:请求携带的参数
	callback:载入成功时的回调函数
	type:设置返回内容格式,xml,html,script,json,text,default
}
//get
$('button').eq(0).click(function(){
    
    
    $.get('http://127.0.0.1:8000/jquery',{
    
    a:100, b:200},function(){
    
    
        console.log(data);
    },'json');
});

//post
$('button').eq(1).click(function(){
    
    
    $.post('http://127.0.0.1:8000/jquery',{
    
    a:100, b:200},function(){
    
    
        console.log(data);
    })
});
2. 通用方法
```javascript //通用方法 $('button').eq(2).click(function(){ $.ajax({ url:'http://127.0.0.1:8000/deplay', data:{a:100,b:200}, type:'GET', //响应体结果类型 dataType: 'json', //成功后的回调 success: function(data){ console.log(data); }, //超时时间 timeout: 2000, //失败回调 error: function(){ console.log('出错了!!'); } }) }) ```

猜你喜欢

转载自blog.csdn.net/weixin_45663697/article/details/110677233