应用程序上的 XMLHttpRequest 失败 [英] XMLHttpRequest failed on App

查看:50
本文介绍了应用程序上的 XMLHttpRequest 失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Ionic 框架和 Parse.com 在我的移动应用程序上运行一个简单的单点示例.代码简单如下:

I'm trying to run a simple singup example on my mobile app using Ionic framework and Parse.com. The code is simple as follows:

Parse.initialize(APP_KEY, JS_KEY);
Parse.User.signUp("my.user", "123456", {}, {
  success: function(user) {
    // Hooray! Let them use the app now.
    console.log('yuhuuu ' + user)
  },
  error: function(user, error) {
    // Show the error message somewhere and let the user try again.
    alert("Error: " + error.code + " " + error.message);
  }
});

此代码在我的浏览器上测试时有效,但当我在手机上运行时,我收到错误代码 100 并显示以下消息:

This code works when I test it on my browser, but when I run it on my cellphone I get an error code 100 with the following message:

我已经尝试过改变我注册的方式(使用对象而不是直接传递用户和密码).还检查了 android 应用程序是否具有访问网络资源的适当权限(没问题).

I've tried already to change the way I singup (usin the object instead of passing user and password directly). Also checked if the android app has proper permissions for accessing web resources (it's OK).

推荐答案

深入挖掘后,我发现了这个 链接,这让我注意到了我的问题的根本原因.Cordova(Ionic 的底层平台之一)将请求限制为仅本地(file://)资源,这使得所有外部请求都失败.

After digging a little deeper I've found this link, which brought to my attention the root cause of my problem. Cordova (one of underlying platforms of Ionic) limits requests to only local (file://) resources, which made all external requests fail.

为了覆盖此行为,您需要使用 白名单插件 并将其设置为您想要的api后端.

In order to overwrite this behaviour you need to use the whitelist plugin and set it up to allow you desired api backend.

这可以通过以下方式实现.

This can be achieved as follows.

首先,将插件添加到项目中.

First, add the plugin to the project.

 cordova plugin add https://github.com/apache/cordova-plugin-whitelist.git

然后将您的后端设置到 config.xml 文件中的白名单.

Then set up your backend to the whitelist at the config.xml file.

<allow-intent href="*://*api.parse.com/*"/>

这篇关于应用程序上的 XMLHttpRequest 失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆