1 Reply Latest reply on Jun 27, 2016 12:54 AM by xkevin

    Problem with ajax request in phonegap with php

    pladecompany

      This is my code php in a server. http://mydomain.com/example.php

       

      <?php

              header('Access-Control-Allow-Origin: *');

              header('Access-Control-Allow-Origin: *');

              header('Access-Control-Allow-Methods: POST,GET,OPTIONS');

              header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept');

              header('Content-Type: application/json');

            
               echo json_encode("ok");

      ?>

       

      in my index.html I have this code javascript with jquery.

       

      $.ajax({

                       type: "GET",

                        url:"http://mydomain.com/example.php",

                       data: {

                            d: 1,

                      },

                      crossDomain: true,

                       cache: false,

                       async: true,

                      contentType: "application/json; charset=utf-8",

                       dataType: "json",

                      timeout: 15000,

                    success: function(data){

                         alert("GET JSON "  +data);

                    },

                       error: function(xhr, status, error) {

                           alert(xhr.status+status+error);

                       }

                   });

       

      And my config.xml is this

       

      <?xml version='1.0' encoding='utf-8'?>

      <widget id="com.phonegap.helloworld" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:gap="http://phonegap.com/ns/1.0">

          <name>Hello World</name>

          <description>

              Hello World sample application that responds to the deviceready event.

          </description>

          <author email="support@phonegap.com" href="http://phonegap.com">

              PhoneGap Team

          </author>

          <content src="index.html" />

          <preference name="DisallowOverscroll" value="true" />

          <preference name="android-minSdkVersion" value="14" />

          <preference name="android-targetSdkVersion" value="15" />

          <plugin name="cordova-plugin-battery-status" source="npm" spec="~1.1.1" />

          <plugin name="cordova-plugin-camera" source="npm" spec="~2.1.1" />

          <plugin name="cordova-plugin-media-capture" source="npm" spec="~1.2.0" />

          <plugin name="cordova-plugin-console" source="npm" spec="~1.0.2" />

          <plugin name="cordova-plugin-contacts" source="npm" spec="~2.0.1" />

          <plugin name="cordova-plugin-device" source="npm" spec="~1.1.1" />

          <plugin name="cordova-plugin-device-motion" source="npm" spec="~1.2.0" />

          <plugin name="cordova-plugin-device-orientation" source="npm" spec="~1.0.2" />

          <plugin name="cordova-plugin-dialogs" source="npm" spec="~1.2.0" />

          <plugin name="cordova-plugin-file" source="npm" spec="~4.1.1" />

          <plugin name="cordova-plugin-file-transfer" source="npm" spec="~1.5.0" />

          <plugin name="cordova-plugin-geolocation" source="npm" spec="~2.1.0" />

          <plugin name="cordova-plugin-globalization" source="npm" spec="~1.0.3" />

          <plugin name="cordova-plugin-inappbrowser" source="npm" spec="~1.3.0" />

          <plugin name="cordova-plugin-media" source="npm" spec="~2.2.0" />

          <plugin name="cordova-plugin-network-information" source="npm" spec="~1.2.0" />

          <plugin name="cordova-plugin-splashscreen" source="npm" spec="~3.2.1" />

          <plugin name="cordova-plugin-statusbar" source="npm" spec="~2.1.2" />

          <plugin name="cordova-plugin-vibration" source="npm" spec="~2.1.0" />

          <plugin name="cordova-plugin-whitelist" source="npm" spec="~1.2.1" />

          <icon src="icon.png" />

          <platform name="android">

              <icon density="ldpi" src="www/res/icon/android/drawable-ldpi-icon.png" />

              <icon density="mdpi" src="www/res/icon/android/drawable-mdpi-icon.png" />

              <icon density="hdpi" src="www/res/icon/android/drawable-hdpi-icon.png" />

              <icon density="xhdpi" src="www/res/icon/android/drawable-xhdpi-icon.png" />

              <icon density="xxhdpi" src="www/res/icon/android/drawable-xxhdpi-icon.png" />

              <icon density="xxxhdpi" src="www/res/icon/android/drawable-xxxhdpi-icon.png" />

              <splash density="land-ldpi" src="www/res/screen/android/drawable-land-ldpi-screen.png" />

              <splash density="land-mdpi" src="www/res/screen/android/drawable-land-mdpi-screen.png" />

              <splash density="land-hdpi" src="www/res/screen/android/drawable-land-hdpi-screen.png" />

              <splash density="land-xhdpi" src="www/res/screen/android/drawable-land-xhdpi-screen.png" />

              <splash density="land-xxhdpi" src="www/res/screen/android/drawable-land-xxhdpi-screen.png" />

              <splash density="land-xxxhdpi" src="www/res/screen/android/drawable-land-xxxhdpi-screen.png" />

              <splash density="port-ldpi" src="www/res/screen/android/drawable-port-ldpi-screen.png" />

              <splash density="port-mdpi" src="www/res/screen/android/drawable-port-mdpi-screen.png" />

              <splash density="port-hdpi" src="www/res/screen/android/drawable-port-hdpi-screen.png" />

              <splash density="port-xhdpi" src="www/res/screen/android/drawable-port-xhdpi-screen.png" />

              <splash density="port-xxhdpi" src="www/res/screen/android/drawable-port-xxhdpi-screen.png" />

              <splash density="port-xxxhdpi" src="www/res/screen/android/drawable-port-xxxhdpi-screen.png" />

          </platform>

          <gap:plugin name="cordova-plugin-whitelist" source="npm"/>

          <access origin="*" />

          <access origin=".*"/>

          <access origin="http://127.0.0.1*"/> <!-- allow local pages -->

          <access origin="http://mydomain.com" />

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

       

       

          <allow-intent href="http://*/*" />

          <allow-intent href="https://*/*" />

          <allow-intent href="tel:*" />

          <allow-intent href="sms:*" />

          <allow-intent href="mailto:*" />

          <allow-intent href="geo:*" />

          <platform name="android">

              <allow-intent href="market:*" />

          </platform>

          <platform name="ios">

              <allow-intent href="itms:*" />

              <allow-intent href="itms-apps:*" />

          </platform>

      </widget>

       

       

      I don't know why this is not working, I really need help.

        • 1. Re: Problem with ajax request in phonegap with php
          xkevin Level 1

          First, what is your error message?

           

          For me I am using this code:

          App code:

          var frm = $('#frm_input');// this is the form

          $.ajax({

                                  type: "POST",

                                  url: "http://www.example.com/app.php",

                                  crossDomain: true,

                                  dataType: 'json',

                                  data: $.trim(frm.serialize()),

                                  timeout: 10000,

                                  success: function(data, status, jqXHR) {

                                      console.log("result is " + data);

                                      console.log(JSON.stringify(jqXHR.responseJSON));

                                  },

                                  error: function(xhr, ajaxOptions, thrownError) {

                                      alert(ajaxOptions + " " + thrownError);

                                  }

                              });

           

           

           

          PHP server code

          header('Content-Type: application/json');

          header('Access-Control-Allow-Origin: *');

           

           

          ///Some code here

          ///connect to my database

          ///and then sql queries

           

          $result_array = array( user_id => $user_id, user_fullname => $user_fullname, user_email => $user_email);

          echo json_encode($result_array); // this will be the response of the server to the app.