wex5的跨域AJAX请求

define(function(require) {
var MD5 = require("$UI/system/lib/base/md5");
var justep = require("$UI/system/lib/justep");
var obj = {};
return {
fnOfAMD2 : function(url, code, callback, content, Header) {
md5 = new MD5();
var key = "";
var timestamp = (new Date()).valueOf();
var parmaerStr = "key=" + key + "&" + code + "×tamp=" + timestamp + "";
var parmaerStrs = parmaerStr.split("&");
parmaerStrs.sort();
var postData = {};
var parmaerStr = "";
var firstEqIndex = -1;
for ( var paramStr in parmaerStrs) {
firstEqIndex = parmaerStrs[paramStr].indexOf("=");
eval("postData." + parmaerStrs[paramStr].substring(0, firstEqIndex) + "=\"" + parmaerStrs[paramStr].substring(firstEqIndex + 1) + "\"");
parmaerStr += parmaerStrs[paramStr] + "&";
}
parmaerStr = parmaerStr.substring(0, parmaerStr.length - 1);
parmaerStr = parmaerStr.replace(/ /g, "%20").replace(/\+/g, "%2B").replace(/\*/g, "%2A");
parmaerStr = encodeURIComponent(parmaerStr)
var sign = md5.hex_md5(parmaerStr);
postData.sign = sign;
var a = url;
// console.log(a);
console.log(postData);
$.support.cors = true;
if (Header == undefined) {
Header = [];
Header[0] = 0;
Header[1] = 1;
}
var ajax = $.ajax({
type : "POST",
url : url,
data : postData,
// async : false,// 同步执行
async : true,// 异步执行
cache : false,
// 登录修改为8秒,其他是30秒
timeout : 30000,
xhrFields : {
withCredentials : true
},
beforeSend : function(xhr) {
xhr.setRequestHeader("sessionkey", Header[0]);
xhr.setRequestHeader("appkey", Header[1]);
},
crossDomain : true,
success : function(data) {
if (isJsonFormat(data) == true) {
obj = eval('(' + data + ')')
console.log(obj);
if (content) {
obj.content = content;
}
callback(obj);
} else {
obj.returnMark = "fail";
callback(obj);
}
// if (isJsonFormat(data.responseText) == true) {
// obj = eval('(' + data.responseText + ')')
// } else {
// obj.returnMark = "fail";
// }
},
error : function(data) {
obj.returnMark = "error";
},
complete : function(data, status) {
if (status == 'timeout') {
obj.returnMark = "error";
callback(obj);
// throw justep.Error.create("请求超时,请稍候再试");
} else if (status == 'success') {
// if (isJsonFormat(data.responseText) == true) {
// obj = eval('(' + data.responseText + ')')
// } else {
// obj.returnMark = "fail";
// }
} else if (status == 'error') {
obj.returnMark = "error";
callback(obj);
// throw justep.Error.create("您的网络出现了问题,请稍候再试");
}
}
});
return obj
},
fnOfAMD1 : function() {
return obj
},
};


function isChina(s) {
var patrn = /[\u4E00-\u9FA5]|[\uFE30-\uFFA0]/gi;
if (!patrn.exec(s)) {
return false;
} else {
return true;
}
}
function isJsonFormat(str) {
try {
$.parseJSON(str);
} catch (e) {
return false;
}
return true;
}
});

你可能感兴趣的:(wex5移动开发,javascript)