Cannot POST data

I have created a small application in which i am posting just one field name district in database using php mysql - my app.js file has code -

angular.module(‘todo’, [‘ionic’])
.controller(‘TodoCtrl’, function($scope,$http) {
$scope.insertdata = function(){
console.log($scope.district);
$http.post(‘insert.php’,{‘district’:$scope.district})
.success(function(data)
{
console.log(“Done”);
})
} });

and index.html has

<-body ng-app=“todo” ng-controller=“TodoCtrl”>
<-form>
<-input type=“text” ng-model=“district”/>
<-input type=“button” value=“submit” ng-click=“insertdata()”/>
<-/form>

and insert.php has:

$data = json_decode(file_get_contents(“php://input”));
$districtname = mysql_escape_string($data->district);
mysql_connect(“localhost”,“root”,“”);
mysql_select_db(“test”);
mysql_query(“Insert into districtapp (districtname) Values ('”.$districtname.“')”);

I have installed codova whitelist and added these lines :

<-access origin=“"/>
<-allow-navigation href="http://
/" />
<-allow-navigation href="https://
/" />
<-allow-navigation href="data:
” />

I run app by ionic serve and get :

Error : POST http://192.168.2.152:8100/insert.php 404 Not Found

Can anyone please help me to solve this Posting issue?

8100 is usually the port where ionic server listens to.
I doubt that your PHP server is running on this port.

Hi thanks for reply - my php port number is 8080 !

OK so you now you must use full PHP url in your post

Url for php is localhost:8080 -

@gmarziou One more thing : If i use “http.get” rather than “http.post” then this get finds file and shows all the file code

I think : This meta for Content Security Policy is not working fine -
meta http-equiv=“Content-Security-Policy” content=“default-src *; style-src ‘self’ ‘unsafe-inline’; script-src * ‘unsafe-inline’ ‘unsafe-eval’”>

<link href="lib/ionic/css/ionic.css" rel="stylesheet"

As :
http://192.168.2.152:35729/livereload -> {“error”:“not_found”,“reason”:“no such route”}

and console shows :

image

Your HTTP server is probably badly configured.

What should i do now?

This is ionic serve, it can happen, if livereload works for you thne ther’s nothing to worry about.

what’s the solution for POST 404 Not found?

Make sure your phone can reach your HTTP server, open browser on phone and try to reach the URL of your PHP script:

  • If it works then use same URL in your code, if you get CORS error (not 404) make sure that CORS is correctly configured on both sides (app and server).
  • if it does not work, learn about proxies

i am not using phone browser - i am using system browser - php with localhost:8080 works fine and this is http://192.168.2.152:8100/ for app - why my system browser is not reaching HTTP server :’( - I am new to all this -

This is a test to validate connection, but OK do as you want.

Instead of insert.php in your app’s javascript use http://192.168.2.152:8080/insert.php. Since the server is missing from the http request the current domain and port will be used as the server address.

ERROR

php is running on 8080 - if i place my insert.php file in xampp or wamp folder and use http://192.168.2.152:8080/insert.php OR http://localhost:8080/insert.php - This works fine but its unable to connect mysql - Why is it and how can i solve this ?

That error looks like it is because the HTTP server is not configured to respond to cross origin resource requests (CORS)

The OPTIONS verb is being used as “pre-flight” request to ensure the server supports the operation you want to do.

See https://en.wikipedia.org/wiki/Cross-origin_resource_sharing

I would check the return values in mysql_connect in the PHP side to see if the connection to the MYSQL server succeeded or not.

@waterishail Thank you for this great help ! :blush:
Apache running on port 80
MySQL running on port 3306
and ionic framework is running on http://192.168.2.152:8100/

and i have to write this http://192.168.2.152:8080/insert.php to post in insert.php file.

Can these different ports create problem after deploying application on android?