2

Right now I need to use phonegap to call on a php script on another webserver to retrieve data from a database i have. Running the script with hard codes retrieves the data correctly but it appears as tho either the ajax call is not returning anything or not even getting the data as post. Currently in the console I am getting "Origin null is not allowed by Access-Control-Allow-Origin. "

    $(document).ready(function() {      
       $('.check').click(function(){
       alert("Step1");
       var thisID    = $(this).attr('id');
       alert(thisID);

       $.ajax({
           type: "POST",
           url: "http://csmaster.sxu.edu/group2/group2/CougarLunch/retrieveColumn.php",
           data: { ID: thisID},
           cache: false,
           async:true,
           datatype: "json",
           success: function(data)
               {
             console.log(data);
             alert(data.ItemID);
              }

          });
          }); 
          });

PHP if it is relevent I doubt it tho

    if(isset($_POST['ID']))
      {
       $ID = $_POST['ID'];
       function retrieve($ID)
         {
          $stmt = $mysqli->query("SELECT * FROM  group2.menu  WHERE ItemID = $ID");

          if($stmt->num_rows) //if there is an ID of this name
            {  
              $row = $stmt->fetch_assoc();
              echo $row;
              print json_encode($row);  

           }
        }
Manish
  • 1,437
  • 2
  • 11
  • 17
Kevin Johnson
  • 218
  • 1
  • 3
  • 10
  • When it's an actual app on the phone, you shouldn't be restricted by the cross domain policy. – ahren Oct 29 '13 at 21:30
  • 1 Why is that? 2. It already isn't working when I launch it in chrome so I'm dubious about that. – Kevin Johnson Oct 29 '13 at 21:31
  • See these questions: http://stackoverflow.com/questions/11641442/phonegap-jquery-ajax-cross-domain-requests-work-in-browser-fail-in-android-sdk http://stackoverflow.com/questions/13920782/phonegap-javascript-sending-cross-domain-ajax-request http://stackoverflow.com/questions/15363005/mobile-apps-cross-domain-ajax – ahren Oct 29 '13 at 21:32
  • have you white listed the domain in phonegap config? – charlietfl Oct 29 '13 at 21:36
  • Just a word of caution about this code `header("Access-Control-Allow-Origin: "*");` because it essentially opens up your PHP file to be run by absolutely any AJAX request so in theory I can write an AJAX call from my website to yours and access whatever data is available on that URL. I recommend messing around with `console.log(document.URL);` and see if you can narrow down the URL/domain that PhoneGap might emulate. Try it from different phones, tables, networks, wifi on/off, etc. – MonkeyZeus Oct 30 '13 at 12:17
  • right now I'm just trying to get it to work in chrome, I have input an else statement to send a default json just to check and it appears as tho the post is never getting sent or received properly – Kevin Johnson Oct 30 '13 at 21:58

2 Answers2

1

Add this line to the top of php file to allow access:

 header("Access-Control-Allow-Origin: "*");
Vicky Gonsalves
  • 11,593
  • 2
  • 37
  • 58
0

you need to pass in AndroidManifest.xml

<access origin="*" />

phonegap doc

Bhumi Shah
  • 9,323
  • 7
  • 63
  • 104