2

I am new in Ionic,Apahce Cordova and I created a simple application which has static list view items but I want to get data from MYSQL table and replace this in my static list. I Google it some one worked on it but I don't know where I should put my php files and I created some php files in Ionic app/www/php files but it doesn't work for me and what is your solution guys? Thank you

virender
  • 4,539
  • 5
  • 27
  • 31
  • you need to put the PHP files on the webserver and use AJAX to pull the data to your cordova APP and populate the ionic list. PHP is not supposed to work inside your mobile. PHP is a server side scripting language that works in conjuction with a webserver. – frank Nov 16 '14 at 06:39
  • Yes I have hosted on my localhost www/test/php files but if want to access the result of this files I got an error: Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost/test/result.php?action=test. This can be fixed by moving the resource to the same domain or enabling CORS. – Gul Muhammad Akbari Nov 16 '14 at 06:46
  • Please search Stackoverflow or the Internet on how to enable CORS in cordova. You will find many links to solve the issue of CORS. The error has nothing to do with PHP, it is a configuration issue. You need to set up cordova and your web server to enable CORS. – frank Nov 16 '14 at 07:03

2 Answers2

2

You can put your php files in localhost or live server.I had the same problem (Cross-Origin Request Blocked) when the app is run in browser.Here are solutions from my experience

1.Test the app in emulator not in browser and change the localhost address to this http://10.0.2.2/test/test.php.This will works fine for me

2.if you are run in android device you cant access from the localhost,so put the files in a live server eg:http://www.testapp.in/test/test.php

shamon shamsudeen
  • 5,466
  • 17
  • 64
  • 129
0

As said above, your PHP files should be hosted on a webserver. And since the resource is not local to your application, you will need $http.jsonp, which allows CORS.

Here's an example of how you'd send a request to a PHP page in AngularJS.

$http.jsonp("http://domain/project/example.php?callback=JSON_CALLBACK&p1=" + $scope.val1 + "&p2=" + $scope.val2)
    .success(function(data) {
       //some function
    })

.error(function(data) {
    console.log("http request failed");
});

OR

For sending requests using jQuery, you can refer this post: https://stackoverflow.com/a/28740155/4412363

Now, you can $_GET the data, then you must have the response in JSONP, also you need to add the callback in your response. It'll look like:

echo $_GET['callback'] . '(' . json_encode($result) . ')';

PS: Here, Ionic takes care of the IPs when you're trying on an emulator. So I just set the url's domain to my local IP address, and it works on all the devices (desktop, emulator, and mobile)

Community
  • 1
  • 1
Keval
  • 3,389
  • 2
  • 24
  • 41