原生JS的ajax,原生ajax傳遞引數格式,ajax引數傳遞,ajax傳遞引數
阿新 • • 發佈:2019-01-27
function success(weiXinConfig) {
console.log("這是微信getConfig回撥函式"+ weiXinConfig);
// weiXinConfig = JSON.parse(weiXinConfig);
// document.getElementById('appid').value = weiXinConfig.appid;
// document.getElementById('projectname').value = weiXinConfig.projectname;
// document.getElementById('version').value = weiXinConfig.version;
}
function upload(weiXinConfig){
//weiXinConfig = JSON.parse(weiXinConfig);
console.log("這是微信upload回撥函式"+ weiXinConfig);
//var weiXinConfig = JSON.parse(weiXinConfig);
//console.log(weiXinConfig);
// document.getElementById('appid').value = weiXinConfig.appid;
// document.getElementById('projectname').value = weiXinConfig.projectname;
// document.getElementById('version').value = weiXinConfig.version;
//var pic_qrCode = document.getElementById('pic_qrCode');
//pic_qrCode.display="block";
}
function ajax_method(url,data,method,success) {
var ajax = new XMLHttpRequest();
if (method=='get') {
if (data) {
url+='?';
url+=data;
}else{
}
ajax.open(method,url);
ajax.setRequestHeader("X-Requested-With","XMLHttpRequest");
ajax.send();
}else{
ajax.open(method,url);
ajax.setRequestHeader("Content-type","application/x-www-form-urlencoded");
ajax.setRequestHeader("X-Requested-With","XMLHttpRequest"); //是否為ajax就靠此句,也是非同步的。
if (data){
ajax.send(data);
}else{
console.log('沒有被髮送的資料!');
//ajax.send();
}
}
ajax.onreadystatechange = function(){
if (ajax.readyState==4 && ajax.status==200){
weiXinConfig = ajax.responseText;
success(weiXinConfig);
}
}
}
//上傳
document.getElementById('btn_uploadProject').onclick = function(){
//document.getElementById('project_config').submit();
var path = document.getElementById('path').value;
var appid = document.getElementById('appid').value;
var projectname = document.getElementById('projectname').value;
var version = document.getElementById('version').value;
var desc = document.getElementById('desc').value;
var strWeiXinConf = "projectDir="+path.trim()+"&appid="+appid.trim()+"&appid="+appid.trim()+"&projectname="+projectname.trim()+"&version="+version.trim()+"&desc="+desc.trim();
console.log("這是ajax傳送給後端的資料:"+strWeiXinConf);
ajax_method('http://www.haodage.com/w.php',strWeiXinConf,'post',upload);
}
//獲取專案配置
document.getElementById('path').onblur = function(){
var path = document.getElementById('path').value;
var strWeiXinConf = "projectDir="+path;
console.log("這是ajax傳送給後端的資料:"+ strWeiXinConf);
ajax_method('http://www.haodage.com/w.php',strWeiXinConf,'post',success);
}