I'm new to Ionic and I'm having trouble getting data from a database in a server.
What I'm trying to do is to connect Ionic to the database in a server through a laravel app.
this.http
.get('http://127.0.0.1:8000/retrieve_data.php')
.subscribe((data : any) =>
{
console.dir(data);
this.items = data;
console.log(this.items);
},
(error : any) =>
{
console.dir(error);
});
Where retrieve_data.php is a file that contains the database connection and query, but nothing shows up in my app.
So I tried doing the same thing but with a local host. If I run the retrieve_data.php file directly from my browser it shows
[{"id":1,"name":"Diego Rodriguez","email":"drodriguezm#fastfit.cl","password":"fastfit","remember_token":null}]
Which is the answer from the query to the db in the localhost. But when I try to do the same thing in Ionic like: this.http.get('http://localhost/retrieve_data.php') I get nothing.
Please help :(
Ohh there may be a problem with ionic, I had a while ago it has something to do with the cross-platform pull from the database.
You can run it and then debug on a device through google chrome and see if there is an error.
If it the cross-platform error ionic provides a solution whereby you use a proxy within the ionic app.
I was doing it wrong, you have to use laravel routes get data from the db. You don't need to create a new file to retrieve data from database, just edit the api.php file and add the routes you need like
Route::get('/users',function(){
User::get();
});
I solved my problem like this.
Related
I have a problem when I make a handling to data.php in my vue-cli project directory:
axios.get('#/data.php')
.then(resp =>
(console.log(resp))
I receive a 404 error. I would like to have API code on data.php and use this data in vue-cli. How can I do this?
The answer is here: https://forum.vuejs.org/t/using-php-with-vue-cli/52842/3
Basically you need to connect your frontend and backend servers in vue.config.js.
I am using Angular 6 and codeigniter project.in the project I am using slider and nav menu-bar are dynamically fetching through REST API. Normally its working perfectly after Login its occurring the CORS error
after login its getting like this
please help me to solve this.
CHOICE 1
Create a proxy.conf.json file in your angular and add following code
{
"/api": {
"target": "http://localhost:3000", //Your Targeted server host
"secure": false
}
}
Go to angular.json and add following code under serve command
"proxyConfig": "proxy.conf.json"
Code looks like
"serve": {
"builder": "#angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "sample:build",
"proxyConfig": "proxy.conf.json"
}
CHOICE 2
Simply go to chrome store and download the extenstion called Allow-Control-Allow-Origin: * , turn it ON. Magic Happens.
DB Query was running well but i think there is a content of a file changed during uploading files to update
I use simple query to get Data from data base but didn't work
it Not running after DB query
$symptoms = DB::table('symptoms')->select('MainSymptoms','Symptom')->get();
return json_encode($symptoms);
probably your database config is wrong.
first you need to see the error so go to config/App.php and change "debug" to true like that:
'debug' => 'true'
and then you will be able to see where is the problem and you can solve it
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 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
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)
I'm working on a site, that is live already, and when I attempt to upload a file - I receive "Status Code: 201 Created", but no content after that.
And when I run the site locally and upload the file - I get the same status code, but after that I get content - JSON with the data of the newly created file.
In both cases I see in the Mongo database that the file is created, and when I attempt to access it through a controller for it through
http://({domain}file/{{file-id}}
I see the file - even on live.
The problem appears to be somewhere after that saving ... and before [[something]] returns the JSOn with the file data ...
... but because everything in Lithium is soooooooooooooooo muuuuuuucccchhhhhhhh automated ... I don't know how to find the problem.
(And I don't want to dump inside the framework itself ... I'm supposed to use the framework, not to debug it! ...)
Well, the problem appeared to be that after the upload the server should return JSON with the data for the file, but on live json_encode() was returning false because of some non-UTF8 text in the object. I managed to workaround this ... and the solution can be found in this question I posted today: json_encode() turn non-UTF8 strings into null, but on live site returns false