首页 > Web开发 > 详细

js ajax请求

时间:2017-10-09 23:16:34      阅读:198      评论:0      收藏:0      [点我收藏+]

Ajax API

var reqUrl = "http://192.168.31.162:8081/obtain/onlineState?name=aa01&password=010203";
var postUrl = "http://192.168.31.162:8081/update/remoteClientInfo";
/**
 * 页面
 *    http://192.168.31.162:8081/http/ajax/Aj01-HttpRequest.html
 * ajax get 请求
 *  http://www.w3school.com.cn/ajax/ajax_xmlhttprequest_send.asp
 */
function testAjaxGet() {
    debugger;
    var xmlhttp;
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.open("GET", reqUrl, true);
//        xmlhttp.open("GET", reqUrl, false);
    xmlhttp.send();
//        console.log(xmlhttp.responseText);
    xmlhttp.onreadystatechange = function () {  // 异步方法回调
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            console.log(xmlhttp.responseText);
        }
    }
}
/**
 * Ref:
 *
 */
function testAjaxPost() {
    debugger;
    var xmlhttp;
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.open("POST", postUrl, true);
//        xmlhttp.open("POST", postUrl, false);
    xmlhttp.setRequestHeader("Content-type","application/json");
    var jsonObj = {
        name:"AAA"
    };
    xmlhttp.send(JSON.stringify(jsonObj));
//        console.log(xmlhttp.responseText);
    xmlhttp.onreadystatechange = function () {  // 异步方法回调
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            console.log(xmlhttp.responseText);
        }
    }
}

参考:

     AJAX - 向服务器发送请求 w3c

js ajax请求

原文:http://www.cnblogs.com/zhen-android/p/7643382.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!