js ajax请求
来源:互联网 发布:福州大数据培训机构 编辑:程序博客网 时间:2024/05/21 05:58
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); } }}
允许跨域的几种方案
/** * 跨域问题解决方案: * P1.使用websocket代理请求; * P2.设置服务器允许跨域请求; * http://www.jb51.net/article/109725.htm * http://blog.csdn.net/flower46273736/article/details/62889077 * http://www.jb51.net/article/95268.htm * P3.JSONP方式;--验证未通过 * http://blog.csdn.net/joyhen/article/details/21631833 * P4.服务器代理连接; * */httpReq.all('*', function(req, res, next) { res.header("Access-Control-Allow-Origin", "http://localhost:63342"); // 指定支持域名 res.header("Access-Control-Allow-Headers", "X-Requested-With"); res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS"); res.header("X-Powered-By",' 3.2.1'); res.header("Content-Type", "application/json;charset=utf-8"); next();});
参考:
AJAX - 向服务器发送请求 w3c
阅读全文
0 0
- 纯js Ajax 请求
- js发送ajax请求
- 纯js Ajax请求
- 纯js Ajax 请求
- js实现ajax请求
- 纯js ajax请求
- js ajax调用请求
- js 登陆ajax请求
- js ajax请求
- js封装ajax请求
- js原生ajax请求
- 关于js中断ajax请求
- js 循环提交ajax请求
- js Ajax跨域请求
- 原生js的ajax请求
- 原生JS实现ajax请求
- AJAX 同步请求的JS
- 原生JS封装AJAX请求
- Js学习之路七 --- 获取时间、charAt()
- java学习初探十七之IO流_FileInputStream
- HDOJ 1004 Let the Balloon Rise
- 【CodeForces】 652B
- vijos1523 [NOI2002]贪吃的九头龙(树形dp)
- js ajax请求
- python学习——爬下网站的所有图片并保存在本地
- 全硬件TCP/IP协议栈学习笔记(第四天:全硬件TCP/IP协议模块学习)
- jquery封装获取表单数据组成Json数据
- 关于Class对象、类加载机制、虚拟机运行时的内存布局的全面解析和推测
- 物联网学习之数据库与物联网搜索引擎详解
- 给出分数数组,得到对应的名次数组,两种情况
- 深入理解Spring MVC 思想
- rmi执行远程方法调用