Ajax写法

①js 写法(get)

var request = null;

if(window.XMLHttpRequest){

request = new XMLHttpRequest()

}else{

request = new ActiveXObject('Microsoft.XMLHTTP')

}

request.open('get', '00-test.txt', true);

request.send()

request.onreadystatechange = function(){

if(request.readyState==4){

if(request.status == 200){

console.log(1)

}

}

}


② js写法(post)

var request = null;

try{

    request = new XMLHttpRequest();

}catch(e){

    request = new ActiveXObject('Microsoft.XMLHTTP')

}

request.open('post', ' http://193.112.122.210/study/login_post.php',true)

request.setRequestHeader('content-type', 'application/x-www-form-urlencoded')

request.send('user=EndEvent& password=123321')

request.onreadystatechange = function(){

    if(request.readyState==4){

        if(request.status==200){

            console.log(1)

        }

    }

}


③ajax写法

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

// $.ajax({

// type:"get",

// url:"http://193.112.122.210/study/login_get.php",

// async:true,

// success:function(data){

// console.log(data)

// },

// error:function(err){

// console.log(1)

// }

// });

// })



④jquery写法(get)

$.get("http://193.112.122.210/json/JSON2.json",function(data){

var arr = JSON.parse(data)

})


⑤jquery写法(post)

// jQuery.post( url [, data ] [, success(data, textStatus, jqXHR) ] [, dataType ] )

//  url: 请求地址

//  data: 发送给服务器的字符串或Key/value键值对

//  success(data, textStatus, jqXHR): 当请求成功后执行的回调函数

//  dataType: 从服务器返回的预期的数据类型。默认:智能判断(xml, json, script, or html)

$.post("http://193.112.122.210/study/login_post.php", {"user":"zhangsan","password":"123321"}, function(data){

  //console.log(data)

  console.log("post")

})

你可能感兴趣的:(Ajax写法)