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

3
  • 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. Commented 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. Commented 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. Commented Nov 16, 2014 at 7:03

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

5 Comments

Why did you test in browser?
Because I want to see result as soon as possible and I do not have enough time.
Its becuse the security issuseof the browser.
you should disable the web security in the browser to access the url refer this (stackoverflow.com/questions/24290149/…)
But will CORS not be an issue when user's app will try to send requests to the web server? I am unsure there, but I wouldn't take a chance with CORS and go with jsonp instead
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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.