JavaScript runtime error: 'Promise' is undefined
JavaScript runtime error: 'Promise' is undefined
我正在开发面向 Android 和 Windows(通用)的 Apache Cordova 应用程序,但是在 Windows 平台上,我在 运行 应用程序时收到以下错误。
0x800a1391 - JavaScript runtime error: 'Promise' is undefined
此问题似乎是 Windows 通用应用程序独有的,在 Android 上运行正常。
有问题的函数如下:
function requestData(ext, params, method) {
return new Promise(function (resolve, reject) {
var req = new XMLHttpRequest();
var url = "https://someurlhere/api/" + ext;
req.open(method, url, true);
req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
req.setRequestHeader("Authorization", token);
req.onreadystatechange = function () {//Call a function when the state changes.
if (req.readyState == 4 && req.status == 200)
resolve(JSON.parse(req.responseText));
console.log(req.status + ' ' + req.statusText);
}
req.onerror = function () {
reject(req.responseText);
}
req.send(params);
});
};
错误在以下行:
return new Promise(function (resolve, reject) {
我猜,因为问题只发生在 Windows 平台上,所以我缺少 Promise
对象。我猜这是我需要包含在项目中的文件。
有什么想法吗?
您应该检查浏览器是否支持 Promise 对象。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
不支持 IE。
Apache Cordova 内置了 WinJS
。所以只需使用:
return new WinJS.Promise(...)
我正在开发面向 Android 和 Windows(通用)的 Apache Cordova 应用程序,但是在 Windows 平台上,我在 运行 应用程序时收到以下错误。
0x800a1391 - JavaScript runtime error: 'Promise' is undefined
此问题似乎是 Windows 通用应用程序独有的,在 Android 上运行正常。
有问题的函数如下:
function requestData(ext, params, method) {
return new Promise(function (resolve, reject) {
var req = new XMLHttpRequest();
var url = "https://someurlhere/api/" + ext;
req.open(method, url, true);
req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
req.setRequestHeader("Authorization", token);
req.onreadystatechange = function () {//Call a function when the state changes.
if (req.readyState == 4 && req.status == 200)
resolve(JSON.parse(req.responseText));
console.log(req.status + ' ' + req.statusText);
}
req.onerror = function () {
reject(req.responseText);
}
req.send(params);
});
};
错误在以下行:
return new Promise(function (resolve, reject) {
我猜,因为问题只发生在 Windows 平台上,所以我缺少 Promise
对象。我猜这是我需要包含在项目中的文件。
有什么想法吗?
您应该检查浏览器是否支持 Promise 对象。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
不支持 IE。
Apache Cordova 内置了 WinJS
。所以只需使用:
return new WinJS.Promise(...)