var NET_ERR = "RES_ERR"; var NET_SUCCESS = "RES_OK"; var NET_NO = "RES_NO"; //未查询到数据 /* 测试版虚拟目录 */ var publishURL = 'LyLSQInterface_war_exploded'; publishURL = 'LyLSQInterface'; /** * 执行网络请求 * @param {Object} data 提交的JSON数据 JSON格式 * @param {Object} ashxName 服务器对应的ashx名称 * @param {Object} waitMsg 等待期间显示的消息内容 * @param {Object} successCallback 获取成功回调 * @param {Object} errorCallback 获取失败回调 * @param {Object} noDataCallback 未获取任何数据回调 */ function sendAjaxJSON(data, ashxName, waitMsg, successCallback, errorCallback, noDataCallback) { getIpAndCom(function(ip, com) { if (ip == "" || com == "") { errorCallback("尚未设置IP地址和端口!"); } else { var url = "http://" + ip + ":" + com + "/lsqservice/phone/" + ashxName + ".ashx"; plus.nativeUI.showWaiting(waitMsg); mui.ajax(url, { data: JSON.stringify(data), dataType: 'json', type: 'post', timeout: 10000, async: true, success: function(result) { plus.nativeUI.closeWaiting(); if (result.res == NET_ERR) { errorCallback(result.data[0].errDes); } else if (result.res == NET_SUCCESS) { successCallback(result.data); } else if (result.res == NET_NO) { noDataCallback(result.data[0].errDes); } }, error: function(xhr, type, errorThrown) { plus.nativeUI.closeWaiting(); errorCallback(errorThrown); } }); } }); } /** * 获取服务器URL地址 * @param {Object} ashxName 服务页名称 * @param {Object} successCallback 成功回调 * @param {Object} errorCallback 失败回调 */ function getServerURL(ashxName, successCallback, errorCallback) { getIpAndCom(function(ip, com) { if (ip == "" || com == "") { errorCallback("尚未设置IP地址和端口!"); } else { var url = "http://" + ip + ":" + com + "/" + publishURL + "/" + ashxName; successCallback(url) } }); } /** * 获取网络图片的服务地址 * @param {Function} callback 成功回调,内容为网络图片的服务地址 */ function getNetImageRootURL(callback) { getIpAndCom(function(ip, com) { if (ip == "" || com == "") { if (callback != undefined) callback(""); } else { if (callback != undefined) callback("http://" + ip + ":" + com + "/" + publishURL + "/") } }); } /** * 通过ajax获取信息 * @param {JSON} options 配置项 */ function sendAjax(options) { /* 统一方式 */ getServerURL(options.url, function(url) { /* 如果设置了等待消息 则显示 */ if (options.waitMessage) plus.nativeUI.showWaiting(options.waitMessage); mui.ajax(url, { data: options.data == undefined ? {} : options.data, dataType: 'json', type: options.type == undefined ? "post" : options.type, timeout: 45000, async: true, success: function(resData) { plus.nativeUI.closeWaiting(); if (resData == null) { console.log("连接失败..."); return; } if (resData.res == 'RES_OK') { if (options.success) options.success(resData.data); } else if (resData.res == 'RES_NO') { if (options.nodata) options.nodata(resData.data[0].msg); } else { if (options.error) options.error(resData.data[0].msg); } }, error: function(xhr, type, errorThrown) { plus.nativeUI.closeWaiting(); /* 打印一下错误信息 */ console.log("AJAX返回错误信息 status = " + xhr.status + " textStatus = " + type); console.log("错误访问url=" + url); if (options.error) options.error(analyzingErrorCode(xhr.status)); } }); }, function(err) { if (options.error) options.error(err); }); } /** * 分析错误代码 * @param {String} code 错误代码 */ function analyzingErrorCode(code) { var res = "未知错误!"; switch (code) { case 0: res = "服务连接异常!"; break; case 203: res = "非权威性信息!"; break; case 401: res = "访问被拒绝!"; break; case 401.1: res = "登录失败!"; break; default: res = "其他错误!" + code; break; } return res; }