LogCat 显示 cordova.js 第 415 行 (函数 checkArgs 如下所示) 抛出一个 Uncaught TypeError 导致我的应用程序中断。
日志显示 Uncaught TypeError: Wrong type for parameter "successCallback"of Device.getInfo: Expected Function, but got Undefined.
错误仅在进行 AJAX 调用时发生...我的 AJAX 调用如下
function checkArgs(spec, functionName, args, opt_callee) {
if (!moduleExports.enableChecks) {
return;
}
var errMsg = null;
var typeName;
for (var i = 0; i < spec.length; ++i) {
var c = spec.charAt(i),
cUpper = c.toUpperCase(),
arg = args[i];
// Asterix means allow anything.
if (c == '*') {
continue;
}
typeName = utils.typeName(arg);
if ((arg === null || arg === undefined) && c == cUpper) {
continue;
}
if (typeName != typeMap[cUpper]) {
errMsg = 'Expected ' + typeMap[cUpper];
break;
}
}
if (errMsg) {
errMsg += ', but got ' + typeName + '.';
errMsg = 'Wrong type for parameter "' + extractParamName(opt_callee || args.callee, i) + '" of ' + functionName + ': ' + errMsg;
// Don't log when running unit tests.
if (typeof jasmine == 'undefined') {
console.error(errMsg);
}
throw TypeError(errMsg);
}
}
我的 AJAX 调用:
var ajax = $.ajax({
type: "POST",
url: "http://www.example.com/tools/api/index.php",
data: api,
dataType:"json",
async:async
});
ajax.done(function( response ) {
// do this
});
ajax.fail(function( jqXHR, textStatus, errorThrown ) {
// do that
});
注意:我正在使用 build.phonegap.com 并使用版本 3.3.0
如有任何想法或建议,我们将不胜感激。
更新: 查看所有使用函数 checkArgs 的源代码,我发现了这个。这是唯一使用 checkArgs 的其他函数
/**
* Get device info
*
* @param {Function} successCallback The function to call when the heading data is available
* @param {Function} errorCallback The function to call when there is an error getting the heading data. (OPTIONAL)
*/
Device.prototype.getInfo = function(successCallback, errorCallback) {
argscheck.checkArgs('fF', 'Device.getInfo', arguments);
exec(successCallback, errorCallback, "Device", "getDeviceInfo", []);
};
为什么会抛出错误...我不确定
最佳答案
原来问题出在提取 window.device
数据上。
我尝试一次记录所有设备 api.device = window.device
... 事实证明设备是一个 cordova 函数,而不是一个附加了静态值的对象。
我不得不将我的代码更改为
api.device = {};
api.device.name = device.name;
api.device.phonegap = device.phonegap;
api.device.platform = device.platform;
api.device.uuid = device.uuid;
api.device.version = device.version;
我刚才在哪里
api.device = device;
希望这可以帮助将来的其他人。
关于javascript - cordova.js 通过 ajax 调用抛出 Uncaught TypeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24664454/