2016-11-30 85 views
0

从我的应用程序我指的是外部API来获取数据。在浏览器和内置版本一切正常,但在仿真器请求被拒绝。在实际的设备应用程序工作得很好。为了调试我需要在模拟器上测试,但我得到这个错误。我检查了每一篇文章,但找不到解决方案。这里是我的配置离子Android模拟器问题

离子INFO

Cordova CLI: 6.4.0 
Ionic CLI Version: 2.1.4 
Ionic App Lib Version: 2.1.2 
OS: Distributor ID: Ubuntu Description: Ubuntu 15.10 
Node Version: v6.3.1 

CONFIG.XML

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<widget id="com.ionicframework.freemusic372670" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0"> 
    <name> 
     FreeMusic 
    </name> 
    <description> 
     An Ionic Framework and Cordova project. 
    </description> 
    <author email="[email protected]" href="http://example.com.com/"> 
     Your Name Here 
    </author> 
    <access origin="*"/> 
    <allow-intent href="*"/> 
    <allow-navigation href="http://*/*"/> 
    <allow-navigation href="https://*/*"/> 
    <allow-navigation href="data:*"/> 
    <allow-navigation href="http://10.11.11.127:8100"/> 
    <allow-navigation href="http://10.11.11.127:8101"/> 
    <allow-navigation href="http://192.168.42.87:8100"/> 
    <content src="index.html"/> 
    <preference name="webviewbounce" value="false"/> 
    <preference name="UIWebViewBounce" value="false"/> 
    <preference name="DisallowOverscroll" value="true"/> 
    <preference name="SplashScreenDelay" value="2000"/> 
    <preference name="FadeSplashScreenDuration" value="2000"/> 
    <preference name="android-minSdkVersion" value="16"/> 
    <preference name="BackupWebStorage" value="none"/> 
    <preference name="SplashScreen" value="screen"/> 
    <feature name="StatusBar"> 
    <param name="ios-package" onload="true" value="CDVStatusBar"/> 
    </feature> 
    <plugin name="cordova-plugin-device" spec="~1.1.3"/> 
    <plugin name="cordova-plugin-console" spec="~1.0.4"/> 
    <plugin name="cordova-plugin-whitelist" spec="~1.3.0"/> 
    <plugin name="cordova-plugin-splashscreen" spec="~4.0.0"/> 
    <plugin name="cordova-plugin-statusbar" spec="~2.2.0"/> 
    <plugin name="ionic-plugin-keyboard" spec="~2.2.1"/> 
    <platform name="android"> 
    <icon src="resources/android/icon/drawable-ldpi-icon.png" density="ldpi"/> 
    <icon src="resources/android/icon/drawable-mdpi-icon.png" density="mdpi"/> 
    <icon src="resources/android/icon/drawable-hdpi-icon.png" density="hdpi"/> 
    <icon src="resources/android/icon/drawable-xhdpi-icon.png" density="xhdpi"/> 
    <icon src="resources/android/icon/drawable-xxhdpi-icon.png" density="xxhdpi"/> 
    <icon src="resources/android/icon/drawable-xxxhdpi-icon.png" density="xxxhdpi"/> 
    <splash src="resources/android/splash/drawable-land-ldpi-screen.png" density="land-ldpi"/> 
    <splash src="resources/android/splash/drawable-land-mdpi-screen.png" density="land-mdpi"/> 
    <splash src="resources/android/splash/drawable-land-hdpi-screen.png" density="land-hdpi"/> 
    <splash src="resources/android/splash/drawable-land-xhdpi-screen.png" density="land-xhdpi"/> 
    <splash src="resources/android/splash/drawable-land-xxhdpi-screen.png" density="land-xxhdpi"/> 
    <splash src="resources/android/splash/drawable-land-xxxhdpi-screen.png" density="land-xxxhdpi"/> 
    <splash src="resources/android/splash/drawable-port-ldpi-screen.png" density="port-ldpi"/> 
    <splash src="resources/android/splash/drawable-port-mdpi-screen.png" density="port-mdpi"/> 
    <splash src="resources/android/splash/drawable-port-hdpi-screen.png" density="port-hdpi"/> 
    <splash src="resources/android/splash/drawable-port-xhdpi-screen.png" density="port-xhdpi"/> 
    <splash src="resources/android/splash/drawable-port-xxhdpi-screen.png" density="port-xxhdpi"/> 
    <splash src="resources/android/splash/drawable-port-xxxhdpi-screen.png" density="port-xxxhdpi"/> 
    </platform> 
</widget> 

index.html中META HEADER

<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'"> 

PLUGINS

cordova-plugin-whitelist 

ERROR

{"data":null,"status":-1,"config":{"method":"GET","transformRequest":[null],"transformResponse":[null],"url":"http://databrainz.com/api/search_api.cgi?qry=metallica&format=json&mh=50&where=mpl","headers":{"Accept":"application/json, text/plain, */*"}},"statusText":""} 

logcat的错误

11-30 11:51:50.971 3179-3179/com.ionicframework.freemusic372670 D/SystemWebChromeClient: http://10.11.11.127:8100/#/tab/dash: Line 0 : XMLHttpRequest cannot load http://databrainz.com/api/search_api.cgi?qry=metallica&format=json&mh=50&where=mpl. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://10.11.11.127:8100' is therefore not allowed access. 

回答

0

您必须启用CORS您的网络服务器。你的错误提到了这一点:

…No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://10.11.11.127:8100' is therefore not allowed access.… 
+0

我用离子运行android -l -c运行。任何想法如何启用? – user2513301

+0

您必须在您的网络服务器配置(apache或nginx)上启用它,而不是在您的设备上。 – RWAM