0

I'm starting a web app on phonegap and i'm testing the distant server connection. And guess what, it does'nt work ! I don't know why. It's working with a browser, but not with the phonegap app i made from the script. I looked on internet, forum, etc. and i founded no explanation. Maybe it's because of the mutualised server i use (OVH.FR) but i'm not sure...

Help ! Thanks !

Here is my files :

index.html

    <!DOCTYPE html>
    <html>
      <head>

<title>Test software</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<script src="./req/jquery/jquery/jquery-2.1.0.min.js"></script>
<script src="./req/jquery/mobile/jquery.mobile-1.4.5.min.js"></script>
<link rel="stylesheet" type="text/css" 
href="./req/jquery/mobile/jquery.mobile-1.4.5.min.css" />

        <script>
    /* ------------------------------------------------ */
    $(document).ready(function(){
    $.ajax({
        url : 'http://test.dexterin.com/intro/introConnexion.php', 
        cache: false,
        type : 'POST',
        dataType : 'xml',
        success : function(xml) { 
          $("#mainPageContent").append("<p>Connected !</p>");
        },
        error : function(xml) {
          $("#mainPageContent").append("<p>No response from ajax</p>");
        }
      }); // Fin ajax
    });
    /* ------------------------------------------------ */
        </script>
      </head>
      <body>
        <div data-role="page" data-theme="a" id="mainPage">
          <div role="content" class="ui-content" id="mainPageContent">
            <p>Initialisation du logiciel ...</p>
          </div>      
        </div>
      </body>
    </html>  

config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<widget xmlns   = "http://www.w3.org/ns/widgets"
    xmlns:gap   = "http://phonegap.com/ns/1.0"
    id          = "com.phonegap.example"
    versionCode = "10"
    version     = "0.0.1" >

<!-- versionCode is optional and Android only -->

  <name>TEST App</name>

  <description>
      A test for phonegap app
  </description>

  <author href="https://build.phonegap.com" email="support@phonegap.com">
      Hardeep Shoker
  </author>

  <access origin="http://test.dexterin.com/" subdomains="true" />

</widget>

introConnexion.php

<?
session_start();
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Methods: POST,GET"); // ,OPTION
header("Access-Control-Allow-Headers: content-type");
header("Access-Control-Allow-Headers: NCZ");
header("Content-type: text/xml;charset=utf-8");
date_default_timezone_set('France/Paris');
echo "<?xml version = \"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>\n";
?>
<root>
<element>OK</element>
</root>
  • [Try the answer to this question regarding cross-domains](https://stackoverflow.com/questions/1201429/jquery-ajax-fails-when-url-is-from-different-server) – Julian Koster Jun 16 '17 at 13:18
  • 1
    It's not what i did with the header('Access-Control-Allow-Origin: *'); in the introConnexion.php file ? (the file on the distant server) – DrPepper Jun 16 '17 at 15:06
  • I used a fonction getXDomainRequest() with : xdr = new XDomainRequest(); Then : xdr.onload = function() {alert("Response : " + xdr.responseText);} xdr.open("GET", "http://test.dexterin.com/intro/introConnexion.php"); xdr.send(); it return an xdr.responseText but empty ... – DrPepper Jun 16 '17 at 17:05
  • You where probably right ! I added the whitelist plug in in the config.xml (see below) and i added a command $.support.cors = true; in the jquery function. Thanks for helping !! – DrPepper Jun 17 '17 at 14:06

1 Answers1

0

You have to install the whitelist plugin to make the access tag work.

Install it with cordova plugin add cordova-plugin-whitelist

jcesarmobile
  • 51,328
  • 11
  • 132
  • 176