jq ajax 和js ajax 两种方式请求数据
2017-10-13 15:26
302 查看
jquery:
(function getCoupon(code, time) {var ajax = jQuery.ajax({
type: "post",
url: '请求地址',
data: {key: '&L!!VGKrJYGDgdfFhtLq', code: code, time: time},
dataType: 'json', //返回数据类型
contentType: "application/json; charset=utf-8", //请求数据编码类型
success: function (data) {
console.log(data);
if (data === 'not_begin') {
return false;
} else if(data === 1) {
jQuery("#Coupon_code").show();
jQuery("#free_au").text(code);
} else {
jQuery("#Coupon_expired").show();
}
}
});
ajax.done(function(){
console.log(ajax);
});
})('code2','2017-10-11 24:00:00');
javascript:
var getCoupon = function(code,time){// var data = {key:'&L!!VGKrJYGDgdfFhtLq',code:code,time: time};
var xmlHttpRequest = null;
if(window.XMLHttpRequest) {
xmlHttpRequest = new XMLHttpRequest();
} else {
if(window.ActiveXObject) {
try {//IE5、6
xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch(e) {
try {
xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {}
}
}
}
xmlHttpRequest.onreadystatechange = function () {
if (xmlHttpRequest.readyState == 4) {
if (xmlHttpRequest.status == 200) {
//200代表成功了
console.log(xmlHttpRequest);
if (xmlHttpRequest.data === 'not_begin') {
return false;
} else if(xmlHttpRequest.data === 1) {
document.getElementById("Coupon_code").style.display = "block";
document.getElementById("free_au").innerText = code;
} else {
document.getElementById("Coupon_expired").style.display = "block";
}
} else {
alert("服务器返回错误!");
}
}
};
xmlHttpRequest.open('POST', 'url', true);
xmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlHttpRequest.send('key=\&L!!VGKrJYGDgdfFhtLq&code=' + code + '&time=' + time);
};
getCoupon('code2','2017-10-11 11:00:00');
相关文章推荐
- iOS 网络请求数据两种请求方式GET,POST
- 两种方式综合请求网络数据
- HttpUrlConnection两种请求数据的方式get和post提交数据
- iOS请求数据两种方式(GET、POST)
- Android使用HttpClient以Post、Get请求服务器发送数据的方式(普通和json)
- 数据仓库的架构主要有星型和雪花型两种方式
- 浏览器在一次 HTTP 请求中,需要传输一个 4097 字节的文本数据给服务端,可以采用那些方式?
- JavaScript第二课-表单及两种数据提交方式
- 在spring mvc中解析输入为json格式的数据有两种方式
- HttpClient get和HttpClient Post请求的方式获取服务器的返回数据
- tomcat7使用get方式请求传json数据出错
- editGrid提交json数据后两种jar包的处理方式net.sf.json.JSONObject与org.json.JSONObject
- Ajax中的get和post两种请求方式的异同
- Adroid开发之解析xml数据的两种方式
- 数据的两种存储方式 cookie 和 webstorage
- 通过物化视图方式同步两服务器中数据表的两种不同方式的比较
- 前端向后台服务器端发送请求并且传送数据的方式
- 【读书笔记】【C++】类外访问私有数据成员的两种方式
- iOS之网上下载数据的两种方式
- swfit3.0 两种方式获取post数据