Can't access the localhost


#1

I am just studying ionic and I’m stacking with this almost a week. what i want is to access the data from localhost

------------------------------------------------------------connecting.php---------------------------------------------

//located at htdocs/xampp/mangkanor-ajax/connecting.php

<?php $fstname = mysql_real_escape_string($_POST['fstname']); $lstname = mysql_real_escape_string($_POST['lstname']); mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("dummy") or die(mysql_error()); mysql_query("INSERT INTO dummy_data (names) VALUES ('$fstname')"); Print "Your information has been successfully added to the database."; ?>

**-----------------------------------------------------------------------------------App.js---------------------------------------------------

.controller(“HttpController”, function($scope,$http){

 $scope.getdata = function()
{
        
          $http.post("http://localhost:80/dummy/connecting.php", {'fstname': 'hahaha', 'lstname': 'hahaha'})
            .success(function(data, status, headers, config){
                console.log("inserted Successfully" + data);
                $scope.firstname = data.fstname;
                $scope.lastname = data.lstname;
            })
            .error(function(data) {
                    alert("error");
            });
           

}

});

--------------------------------------------------------------------index.html---------------------------------------------------------------------

  <ion-content ng-controller = "HttpController">
    <button class = "button" ng-click="getdata()">send</button>
    <br>
    name: {{firstname}} {{lastname}} 
  </ion-content>

i’m accesing it through genymotion emulator. and i always get an alert Error!


#2

I dont know, what the genymotion emulator ist, but first question:
Does php insert the data into mySQL correct?

If it is so, i think the problem could be, that you print a plain string, and maybe your PHP Version does not make a compatible header…

Maybe you try something like:

$data = "Your information has been successfully added to the database.";
header('Content-Type: application/json');
echo json_encode($data);

With a JSON header and JSON Response, you should be save on header Problems / communication conflicts…


#3

Yes sir it insert correctly. PHP Version 5.6.8

  • my script located

I’ll try this
$data = “Your information has been successfully added to the database.”;
header(‘Content-Type: application/json’);
echo json_encode($data);

$fstname = mysql_real_escape_string($_POST['fstname']);
$lstname = mysql_real_escape_string($_POST['lstname']);

mysql_connect("localhost", "root", "") or die(mysql_error()); 
mysql_select_db("dummy") or die(mysql_error()); 
mysql_query("INSERT INTO dummy_data (names) VALUES ('$fstname')"); 
Print "Your information has been successfully added to the database."; 

and im getting the same error


#4

Thats kind of weird.
I could not see anything, that is wrong …

I suggest, that you take a look to the requests and responses in the browser debugging on the network tab, and check send and response header, and it the server already delivers something like an error.

And maybe:

.error(function(data) {
    alert("error");
    console.log(data);
 });

could give you a further hint, whats going wrong…


#5

Do you mean ionic serve?
i’m running it on emulator and it gives me this.


#6

Oh. No - i meant to try to debug it in a real browser… An in browser debug tools, the network tab.
There you could take a look at the detailled requests and responses and the headers and so on.


#8