Ionic framework and php mysql

2019-05-22 20:40发布

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

2条回答
够拽才男人
2楼-- · 2019-05-22 20:43

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)

查看更多
SAY GOODBYE
3楼-- · 2019-05-22 20:54

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

查看更多
登录 后发表回答