码迷,mamicode.com
首页 > Web开发 > 详细

js ajax请求

时间:2017-10-10 00:13:15      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:jaxp   style   code   app   chrome   请求   异步   logs   ie7   

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请求

标签:jaxp   style   code   app   chrome   请求   异步   logs   ie7   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!