Join the Stack Overflow Community
Stack Overflow is a community of 6.3 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I want to get specific value from server side. Therefore I am using $http to pass variable from front-end (Angularjs javascript) to backend(php). After server side (php) get the value from front-end. It will run sql query to get data from mysql and return the data to front-end. However, in my console of front-end, it shows undefined error. The following is my code:

Front-end Javascript

$http({
    method: "post",
    url: "http://localhost/php/UpdateLastLogin.php",
    data: {
        user_name: user_name,
        CurrentTimestamp: CurrentTimestamp
        },
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
}).success(function(response) {
    console.log(response);
}).error(function(response) {
    console.log(response);
});

Backend PHP

<?php
    header("Access-Control-Allow-Origin: *");
    header("Access-Control-Allow-Credentials: true");
    header("Access-Control-Allow-Methods: POST, GET, OPTIONS");
    header("Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With");
    header('P3P: CP="CAO PSA OUR"'); 
    header("Content-Type: application/json; charset=utf-8");


    $postdata = file_get_contents("php://input");
    $request = json_decode($postdata);
    @$CurrentTimestamp = $request->CurrentTimestamp;
    @$user_name = $request->user_name;

    $servername = "localhost";
    $username = "jack";
    $password = "1234";
    $dbname = "daikinchat";

    $CurrentTimestamp = '"' . $CurrentTimestamp . '"';
    //$user_name = '"' . $user_name . '"';
    $user_name = "Lisa Wong";

    $conn = new mysqli($servername, $username, $password, $dbname);
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        }

    $query = "SELECT last_login_timestamp FROM user_directory WHERE username = '$user_name'";
    $result = mysqli_query($conn, $query);
    $row = mysqli_fetch_row($result);

    echo $row[0];

?>
share|improve this question
    
which line shows? – Sajeetharan Oct 10 at 7:16
    
@Sajeetharan error(function(response) { console.log(response);} – chan yoonghon Oct 10 at 7:18
    
try content type application/json in your $http request – Divyesh Savaliya Oct 10 at 7:21

You request should be like this, so that response will be a json,

$http({
    method: "post",
    url: "http://localhost/php/UpdateLastLogin.php",
    data: {
        user_name: user_name,
        CurrentTimestamp: CurrentTimestamp
        },
    headers: { 'Content-Type': 'application/json' }
}).success(function(response) {
    console.log(response);
}).error(function(response) {
    console.log(response);
});
share|improve this answer
    
I want my response in String instead of json. can I do that? – chan yoonghon Oct 10 at 7:28
    
yes you can do . console.log(JSON.stringify(response)) – Sajeetharan Oct 10 at 7:29
    
did you get it sorted? – Sajeetharan Oct 10 at 7:36
    
I think the main problem is I post object to backend but in my php echo is a string instead of object. Therefore, it come out with SyntaxError: Unexpected token a in JSON at position 0. Can my response accept String? – chan yoonghon Oct 10 at 7:37
    
now you are getting response on front end? – Sajeetharan Oct 10 at 7:39

The problem may be in your $http call, my $http calls look like:

$http({
            method: 'post',
            url:'http://localhost/php/UpdateLastLogin.php',
            data: {...},
            config: 'Content-Type: application/json;'
        }).then(function (response) {
            console.log(response);
        }, function (response) {
            console.log(response);
        });

If I'm not wrong, you can use both, headers or config, but seems like you aren't using properly the attribute headers, the documentation says:

  • headers – {function([headerName])} – Header getter function.
  • config – {Object} – The configuration object that was used to generate the request.

So try to change your 'Content-Type' to 'application/json', because that's what you are sending in you http request body. If it doesn't work, change headers to config to test.

If it doesn't work either, I'd use any tool like, 'postman' to check the API, and make sure that it's working, and try to debug the PHP code (I can't help you with this part)

share|improve this answer
    
I think the main problem is I post object to backend but in my php echo is a string instead of object. Therefore, it come out with SyntaxError: Unexpected token a in JSON at position 0. Can my response accept String? – chan yoonghon Oct 10 at 7:37
    
You can use JSON.stringfy(json_object) to get a string, and JSON.parse(string_object) to get a json again from that string. So make sure you have you object casted to json or string before use it... – Alberto Martinez Oct 10 at 7:45
    
I I just notices, modify your $http call, use this one: ` $http({ method: "post", url: "localhost/php/UpdateLastLogin.php";, data: { user_name: user_name, CurrentTimestamp: CurrentTimestamp }, headers: { 'Content-Type': 'application/json' } }.then(function (response) { console.log(response); }, function (response) { console.log(response); }); ` – Alberto Martinez Oct 10 at 7:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.