标签:设置 open 必须 request pos php soft json ready
1 //发送get请求 2 function get_http_request(url, callback) 3 { 4 var httpRequest;//第一步:建立所需的对象 5 if (window.XMLHttpRequest) { 6 httpRequest = new XMLHttpRequest(); 7 } 8 else { 9 httpRequest = new ActiveXObject("Microsoft.XMLHTTP"); 10 } 11 httpRequest.open(‘GET‘, url, true);//第二步:打开连接 将请求参数写在url中 ps:"./Ptest.php?name=test&nameone=testone" 12 httpRequest.send();//第三步:发送请求 将请求参数写在URL中 13 14 // 获取数据后的处理程序 15 httpRequest.onreadystatechange = function () { 16 if (httpRequest.readyState === 4 && httpRequest.status === 200) { 17 var data_dic = JSON.parse(httpRequest.responseText); 18 callback(data_dic); 19 } 20 }; 21 return httpRequest; 22 } 23 24 //发送post请求 25 function post_http_request(url, callback, data) { 26 var httpRequest;//第一步:建立所需的对象 27 if (window.XMLHttpRequest) { 28 httpRequest = new XMLHttpRequest(); 29 } 30 else { 31 httpRequest = new ActiveXObject("Microsoft.XMLHTTP"); 32 } 33 httpRequest.open(‘POST‘, url, true); //第二步:打开连接/***发送json格式文件必须设置请求头 ;如下 - */ 34 httpRequest.setRequestHeader("Content-type", "application/json");//设置请求头 注:post方式必须设置请求头(在建立连接后设置请求头)var obj = { name: ‘zhansgan‘, age: 18 }; 35 httpRequest.send(data); //发送请求 将json写入send中,httpRequest.send(JSON.stringify(obj)); 36 /** 37 * 获取数据后的处理程序 38 */ 39 httpRequest.onreadystatechange = function () {//请求后的回调接口,可将请求成功后要执行的程序写在其中 40 if (httpRequest.readyState == 4 && httpRequest.status == 200) {//验证请求是否发送成功 41 var data_dic = JSON.parse(httpRequest.responseText); 42 callback(data_dic); 43 } 44 }; 45 }
标签:设置 open 必须 request pos php soft json ready
原文地址:https://www.cnblogs.com/peifuqiang/p/11676537.html