js中的AJAX

js中ajax get請求的寫法 
var
xhr=new XMLHttpRequest(); xhr.open('get','http://rap2api.taobao.org/app/mock/229494/news?content='+ inp.value) xhr.send(); xhr.onreadystatechange=function () { if (xhr.readyState==4) { // console.log(xhr.responeText } }
js中post請求的寫法
var
xhr = new XMLHttpRequest(); xhr.open('post',"http://192.168.1.5/archives/register.php",true); // 若是是post請求 必須加上請求頭 xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded"); xhr.send("userName="+user.value+'&passWord='+pass.value); xhr.onreadystatechange =function(){ if (xhr.readyState==4) { var obj = JSON.parse(xhr.responseText); console.log(obj.responseCode); if (obj.responseCode=="0") { window.location.href = "denglu.html" } } }
相關文章
相關標籤/搜索