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
-
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– frank2014-11-16 06:39:18 +00:00Commented Nov 16, 2014 at 6: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 localhost/test/result.php?action=test. This can be fixed by moving the resource to the same domain or enabling CORS.Gul Muhammad Akbari– Gul Muhammad Akbari2014-11-16 06:46:30 +00:00Commented Nov 16, 2014 at 6: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– frank2014-11-16 07:03:23 +00:00Commented Nov 16, 2014 at 7:03
2 Answers
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
5 Comments
jsonp insteadAs 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)