2

I know this question has been asked many, many times online. I've run out of luck trying everything there is.

I'm trying to POST data to a server and get back a simple response. Obviously I would need to install the cordova whitelist plugin to achieve accessing external sources from the app, so I installed it.

The error I get back is: Failed to load resource: net::ERR_NAME_NOT_RESOLVED

Project Info:

  • This is an Onsen UI app
  • Cordova CLI 6.5.0
  • Android Version 6.1.2

Installed Plugins:

  • cordova-plugin-compat@1.1.0
  • cordova-plugin-file@~4.3.2
  • cordova-plugin-splashscreen@4.0.2
  • cordova-plugin-camera@2.4.0
  • cordova-plugin-whitelist@1.3.3-dev

Meta Tag:

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

Tools I'm using: Windows 10, Visual Studio 2015

What I have done:

  1. Uninstalled and Reinstalled the Whitelist plugin, using Visual Studio

  2. Uninstalled the plugin and reinstalled via Cordova CLI

    cordova plugin add cordova-plugin-whitelist

  3. Uninstalled and reinstalled plugin via Github

  4. I've even updated Cordova to 6.5.0 from 6.2.0

  5. I created a simple GET request to see if it works and still nothing works.

  6. Cleared the Cordova Cache in Visual Studio.

Javascript code I used:

$http({
    method: 'GET', url: "http://mywebsite.com/simple_get.php"
}).then(
   function (response) {
       alert(JSON.stringify(response));
   },
   function (response) {
       alert(JSON.stringify(response));
   }
);

jQuery Code also tried:

var settings = {
    "async": true,
    "crossDomain": true,
    "url": "http://mywebsite.com/demo.php",
    "method": "GET",
    "headers": {
        "content-type": "application/x-www-form-urlencoded",
        "cache-control": "no-cache"
    },
    "data": {}
}

jQuery.ajax(settings).done(function (response) {
    console.log(response);
});

I have no idea how to debug anymore and get to the problem, any help would be greatly appreciated... This is the final step for my app to be done :(

Thanks

Rizo
  • 54
  • 8

2 Answers2

0

Edit 2:

Try this code anywhere above the Ajax function.

$(document).ajaxError(function(e, jqxhr, settings, exception) { 
    if (jqxhr.readyState == 0 || jqxhr.status == 0) {
         return; 
    }
 });

This will catch the status 0 error and allow jQuery to ignore it and continue with the Ajax request.

Use it just for debugging purposes.

Edit

Try the following.

$.ajax({
   url: 'YourRestEndPoint',
   headers: {'yourHeaderKey': 'yourHeaderValue',
'yourHeaderKey2': 'yourHeaderValue2'},
   method: 'POST',
   data: {},
   success: function(data){
        console.log('succes: '+data);
    },
    error: function (jqXHR, textStatus, errorThrown){

        console.log(jqXHR + " " + textStatus + " " + errorThrown);

        console.log(jqXHR.status);

    }
});

This should log errors a bit better.

I see from your question you are trying to POST data to the server, however in the code you use a GET request. Does the endpoint accept both POST and GET or just POST.

Original If your are having a 'Name Not Resolved' error have you ensured the URL is correct? Are you running the endpoint on a server you have control over? Is it hosted by you or a third party? Is there a problem with your DNS lookup? If you try a known working endpoint does the same code work correctly?

Could be caused by a lot or various factors.

The whitelist plugin should automatically be installed on the latest version of Cordova.

L Balsdon
  • 995
  • 8
  • 12
  • The URL is correct, I tried hitting the URL with postman, and there is a response. I have full control over the endpoint server, DNS lookup is fine. The endpoint works fine locally running inside of Ripple Emulator. How can I debug this ? – Rizo Apr 16 '17 at 21:29
  • See my edit for better error logging. Also try my format for your Ajax request using jQuery. – L Balsdon Apr 17 '17 at 07:10
  • It accepts both POST and GET, I tried the JS you sent me. The output for jqHXR is: `[object Object] controllers.js (111,25) { [functions]: , __proto__: { }, readyState: 0, status: 0, statusText: error }` – Rizo Apr 17 '17 at 09:56
  • I have added a line to log the status. Can you try it and tell me what you get. – L Balsdon Apr 17 '17 at 10:08
  • It returns back a zero (0) in my above comment you'll see the status is logged as **status: 0** – Rizo Apr 17 '17 at 10:28
  • Sorry yes that would be write. Can you post a copy of your config.xml? – L Balsdon Apr 17 '17 at 10:58
0

This is my config.xml

<?xml version="1.0" encoding="utf-8"?>
<widget id="au.com.myapp" version="1.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:vs="http://schemas.microsoft.com/appx/2014/htmlapps">
  <name> MyApp</name>
  <description>fancy user interfaces for hybrid mobile applications. It uses uses Apache Cordova to help you build an app that targets multiple mobile platforms: Android, iOS, Windows, and Windows Phone.</description>
  <author email="admin@myappdomain.com" href="http://myappdomain.com">Author</author>
  <content src="index.html" />
  <access origin="*" />
  <allow-intent href="*" />
  <allow-intent href="http://*/*" />
  <allow-intent href="https://*/*" />
  <allow-intent href="tel:*" />
  <allow-intent href="sms:*" />
  <allow-intent href="mailto:*" />
  <allow-intent href="geo:*" />
  <allow-navigation href="*" />
  <allow-intent href="itms:*" />
  <allow-intent href="itms-apps:*" />
  <vs:features />
  <preference name="windows-target-version" value="8.1" />
  <preference name="windows-phone-target-version" value="8.1" />
  <preference name="DisallowOverscroll" value="true" />
  <preference name="Orientation" value="portrait" />
  <preference name="loglevel" value="DEBUG" />
  <preference name="AndroidLaunchMode" value="singleTop" />
  <preference name="ErrorUrl" value="" />
  <preference name="Fullscreen" value="True" />
  <preference name="KeepRunning" value="true" />
  <preference name="SplashScreen" value="screen" />
  <preference name="AutoHideSplashScreen" value="false" />
  <preference name="SplashScreenDelay" value="20000" />
  <preference name="FadeSplashScreen" value="false" />
  <preference name="FadeSplashScreenDuration" value=".25" />
  <preference name="ShowSplashScreenSpinner" value="false" />
  <preference name="AllowInlineMediaPlayback" value="false" />
  <preference name="BackupWebStorage" value="cloud" />
  <preference name="EnableViewportScale" value="false" />
  <preference name="KeyboardDisplayRequiresUserAction" value="true" />
  <preference name="MediaPlaybackRequiresUserAction" value="false" />
  <preference name="SuppressesIncrementalRendering" value="false" />
  <preference name="TopActivityIndicator" value="gray" />
  <preference name="GapBetweenPages" value="0" />
  <preference name="PageLength" value="0" />
  <preference name="PaginationBreakingMode" value="page" />
  <preference name="PaginationMode" value="unpaginated" />
  <feature name="LocalStorage">
    <param name="ios-package" value="CDVLocalStorage" />
  </feature>
  <preference name="UIWebViewDecelerationSpeed" value="normal" />
  <preference name="monaca:AndroidIsPackageNameSeparate" value="false" />
  <preference name="monaca:targetFamilyiPhone" value="1" />
  <preference name="monaca:targetFamilyiPad" value="1" />
  <platform name="android">
    <icon density="ldpi" src="resources/android/icon/drawable-ldpi/icon.png" />
    <icon density="mdpi" src="resources/android/icon/drawable-mdpi/icon.png" />
    <icon density="hdpi" src="resources/android/icon/drawable-hdpi/icon.png" />
    <icon density="xhdpi" src="resources/android/icon/drawable-xhdpi/icon.png" />
  </platform>
  <platform name="android">
    <splash density="land-hdpi" src="resources/android/drawable-land-hdpi/screen.png" />
    <splash density="land-ldpi" src="resources/android/drawable-land-ldpi/screen.png" />
    <splash density="land-mdpi" src="resources/android/drawable-land-mdpi/screen.png" />
    <splash density="land-xhdpi" src="resources/android/drawable-land-xhdpi/screen.png" />
    <splash density="port-hdpi" src="resources/android/drawable-port-hdpi/screen.png" />
    <splash density="port-ldpi" src="resources/android/drawable-port-ldpi/screen.png" />
    <splash density="port-mdpi" src="resources/android/drawable-port-mdpi/screen.png" />
    <splash density="port-xhdpi" src="resources/android/drawable-port-xhdpi/screen.png" />
  </platform>
  <platform name="ios">
    <splash height="480" src="res/screen/ios/Default~iphone.png" width="320" />
    <splash height="960" src="res/screen/ios/Default@2x~iphone.png" width="640" />
    <splash height="1024" src="res/screen/ios/Default-Portrait~ipad.png" width="768" />
    <splash height="2048" src="res/screen/ios/Default-Portrait@2x~ipad.png" width="1536" />
    <splash height="768" src="res/screen/ios/Default-Landscape~ipad.png" width="1024" />
    <splash height="1536" src="res/screen/ios/Default-Landscape@2x~ipad.png" width="2048" />
    <splash height="1136" src="res/screen/ios/Default-568h@2x~iphone.png" width="640" />
    <splash height="1334" src="res/screen/ios/Default-667h.png" width="750" />
    <splash height="2208" src="res/screen/ios/Default-736h.png" width="1242" />
    <splash height="1242" src="res/screen/ios/Default-Landscape-736h.png" width="2208" />
    <splash src="res/screen/ios/Default@2x~universal~anyany.png" />
    <splash src="res/screen/ios/Default@2x~universal~comany.png" />
    <splash src="res/screen/ios/Default@2x~universal~comcom.png" />
    <splash src="res/screen/ios/Default@3x~universal~anyany.png" />
    <splash src="res/screen/ios/Default@3x~universal~anycom.png" />
    <splash src="res/screen/ios/Default@3x~universal~comany.png" />
  </platform>
  <plugin name="cordova-plugin-camera" spec="~2.4.0" />
  <plugin name="cordova-plugin-compat" version="1.1.0" />
  <plugin name="cordova-plugin-file" spec="~4.3.2" />
  <plugin name="cordova-plugin-inappbrowser" version="1.7.0" />
  <plugin name="cordova-plugin-splashscreen" version="4.0.2" />
  <plugin name="cordova-plugin-whitelist" version="1.3.2" />
</widget>
Rizo
  • 54
  • 8
  • This looks fine. The should allow you to access any URL outside of the app. – L Balsdon Apr 17 '17 at 12:28
  • I've tried everything.. I created a blank application from scratch and just added the whitelist plugin with a simple GET and that also doesn't work. could it be a PC configuration ? – Rizo Apr 17 '17 at 13:51
  • How are you testing? On a device or simulator? Can you test in chrome and inspect the console? – L Balsdon Apr 17 '17 at 14:25
  • 1
    On Chrome it works fine, Also through Ripple it works fine.. It only gives an error when I install the apk on the actual device – Rizo Apr 17 '17 at 14:26
  • I have updated my answer with information on catching and ignoring status 0 errors. – L Balsdon Apr 17 '17 at 14:32
  • I tried your code above and unfortunately it still didn't work... However, I created a Phonegap Build account, I uploaded the project, installed the app on my device and it works perfectly! Does this mean it's something to do with my visual studio configuration ? – Rizo Apr 17 '17 at 14:45