首页 >> js开发 >> js代码javascript前端和后台进行数据交互方法示例
js代码javascript前端和后台进行数据交互方法示例
发布时间: 2021年1月13日 | 浏览:
| 分类:js开发
在开发中遇到了在没有jQuery的情况下需要与后台进行部分数据的交互,查找了部分资料使用JavaScript实现了操作,记录一下。
//获取XMLHttpRequest对象用于与后台交互数据
//获取XMLHttpRequest对象用于与后台交互数据
function getXHR(){
var xmlHttp;
try {
xmlHttp=new XMLHttpRequest();//新版本浏览器
}catch(e)
{
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");//老版本浏览器
}
catch(e)
{
alert("你的浏览器不支持ajax");
return false;
}
}
}
return xmlHttp;
}
function getXHR(){
var xmlHttp;
try {
xmlHttp=new XMLHttpRequest();//新版本浏览器
}catch(e)
{
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");//老版本浏览器
}
catch(e)
{
alert("你的浏览器不支持ajax");
return false;
}
}
}
return xmlHttp;
}
function check() {
var pass = document.getElementById("pass").value;
//1/得到xhr对象
var xhr=getXHR();
//2.注册状态变化监听器
xhr.onreadystatechange=function(){
if(xhr.readyState==4) {
if(xhr.status==200) {
var obj = document.getElementById("checkPass");
if("true" == xhr.responseText){
obj.innerHTML = "验证通过";
obj.style.color = "green";
}else{
obj.innerHTML = "原密码输入错误!";
obj.style.color = "brown";
return;
}
}
}
}
//3.建立与服务器的连接(post请求方式,也可以使用get请求方式)
xhr.open("post","请求数据的地址");
xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded');
//4.向服务器发出请求(使用post请求方式将数据发往后台)
xhr.send("pass="+pass);
}
function check() {
var pass = document.getElementById("pass").value;
//1/得到xhr对象
var xhr=getXHR();
//2.注册状态变化监听器
xhr.onreadystatechange=function(){
if(xhr.readyState==4) {
if(xhr.status==200) {
var obj = document.getElementById("checkPass");
if("true" == xhr.responseText){
obj.innerHTML = "验证通过";
obj.style.color = "green";
}else{
obj.innerHTML = "原密码输入错误!";
obj.style.color = "brown";
return;
}
}
}
}
//3.建立与服务器的连接(post请求方式,也可以使用get请求方式)
xhr.open("post","请求数据的地址");
xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded');
//4.向服务器发出请求(使用post请求方式将数据发往后台)
xhr.send("pass="+pass);
}
//获取XMLHttpRequest对象用于与后台交互数据
//获取XMLHttpRequest对象用于与后台交互数据
function getXHR(){
var xmlHttp;
try {
xmlHttp=new XMLHttpRequest();//新版本浏览器
}catch(e)
{
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");//老版本浏览器
}
catch(e)
{
alert("你的浏览器不支持ajax");
return false;
}
}
}
return xmlHttp;
}
function getXHR(){
var xmlHttp;
try {
xmlHttp=new XMLHttpRequest();//新版本浏览器
}catch(e)
{
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");//老版本浏览器
}
catch(e)
{
alert("你的浏览器不支持ajax");
return false;
}
}
}
return xmlHttp;
}
function check() {
var pass = document.getElementById("pass").value;
//1/得到xhr对象
var xhr=getXHR();
//2.注册状态变化监听器
xhr.onreadystatechange=function(){
if(xhr.readyState==4) {
if(xhr.status==200) {
var obj = document.getElementById("checkPass");
if("true" == xhr.responseText){
obj.innerHTML = "验证通过";
obj.style.color = "green";
}else{
obj.innerHTML = "原密码输入错误!";
obj.style.color = "brown";
return;
}
}
}
}
//3.建立与服务器的连接(post请求方式,也可以使用get请求方式)
xhr.open("post","请求数据的地址");
xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded');
//4.向服务器发出请求(使用post请求方式将数据发往后台)
xhr.send("pass="+pass);
}
function check() {
var pass = document.getElementById("pass").value;
//1/得到xhr对象
var xhr=getXHR();
//2.注册状态变化监听器
xhr.onreadystatechange=function(){
if(xhr.readyState==4) {
if(xhr.status==200) {
var obj = document.getElementById("checkPass");
if("true" == xhr.responseText){
obj.innerHTML = "验证通过";
obj.style.color = "green";
}else{
obj.innerHTML = "原密码输入错误!";
obj.style.color = "brown";
return;
}
}
}
}
//3.建立与服务器的连接(post请求方式,也可以使用get请求方式)
xhr.open("post","请求数据的地址");
xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded');
//4.向服务器发出请求(使用post请求方式将数据发往后台)
xhr.send("pass="+pass);
}
相关文章:
- jsVant picker 多级联动操作js大全
- js解决antd 表单设置默认值initialValue后验证失效的问题js大全
- js在antd Form表单中select设置初始值操作js大全
- js微信小程序实现单个或多个倒计时功能js大全
- js在antd4.0中Form使用initialValue操作js大全
- jsvue+iview使用树形控件的具体使用js大全
- jsVue中使用Echarts仪表盘展示实时数据的实现js大全
- jsReact Ant Design树形表格的复杂增删改操作js大全
- js在vue中使用vant TreeSelect分类选择组件操作js大全
- jsvantUI 获得piker选中值的自定义ID操作js大全