js ajax请求
Posted zhen
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js ajax请求相关的知识,希望对你有一定的参考价值。
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请求的主要内容,如果未能解决你的问题,请参考以下文章