js使用XMLHttpRequest实现跨域请求 POST请求
来源:互联网 发布:linux新建用户组 编辑:程序博客网 时间:2024/06/05 10:08
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title> <script src="js/jquery.js"></script></head><body><button onclick="xmlhttp()">xmlhttp</button></body><script> function getHttpObj() { var httpobj = null; try { httpobj = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { httpobj = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e1) { httpobj = new XMLHttpRequest(); } } return httpobj; } function xmlhttp() {// var xhr = new XMLHttpRequest(); var xhr = getHttpObj(); xhr.open("post", "http://www.abcd.com/*****", true); xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");//缺少这句,后台无法获取参数 xhr.onreadystatechange = function() { console.log(xhr.responseText); if (xhr.readyState == 4 && xhr.status == 200) { console.log(xhr.responseText); } }; var content = "appid=11111&sign=222222222"; xhr.send(content); }</script></html>
使用js编写跨域http的post请求
注意,服务器端须设置设置response'Access-Control-Allow-Origin',
有些朋友可能会用ajax的jsonp方法,但是jsonp只能提供get请求,对于限制post请求的接口则无法适用。
关于jsonp的实现原理,参照http://www.jb51.net/article/75669.htm
0 0
- js使用XMLHttpRequest实现跨域请求 POST请求
- {{js跨域请求解决方案续}}Cross-domain XMLHttpRequest(使用Apache mod_rewrite实现跨域请求)
- 原生js使用xmlhttpRequest实现ajax请求
- XMLHttpRequest发送POST请求
- js使用post请求
- JS实现使用POST方式发送请求
- Xmlhttprequest请求跨域
- {{js跨域请求解决方案}}Use a Web Proxy for Cross-Domain XMLHttpRequest Calls(使用服务器端代理实现Ajax跨域请求)
- 使用js进行post请求
- 使用HttpURLConnection实现post请求
- 习惯了使用jQuery的ajax方法,看看原生js使用xmlhttpRequest实现ajax请求
- POST 请求的跨域实现
- JS实现跨域请求
- Js XMLHTTPRequest 实现GET请求提交和返回内容获取
- 解决get请求乱码问题,js实现post请求
- XmlHttpRequest的Post请求方式示例
- JS使用POST方式提交请求
- 使用JS和Ajax发出异步请求,XmlHttpRequest对象详解
- forEach实现原理
- C语言中宏函数定义的注意点
- Java重载
- (一)安装CentOS7
- 反射
- js使用XMLHttpRequest实现跨域请求 POST请求
- 更改nginx的网站根目录
- Android 生成keystore和ecplise 发布release apk
- RPM和YUM常用命令
- ccf试题 日期计算
- SpringBoot实践
- react-redux部分关键源码解析
- 太用力的人跑不远
- MySQL数据常用命令大集合