1

php json:encode returning undefined on http call on angular controller on server but works well on the localhost.

if( hash_equals($hashed_password, crypt($password, $hashed_password))){
    $pass = "true";
    $result = array('pass' => $pass, 'FirstName' => $f_name,
    'LastName'  => $l_name,'id' => $id);
}else{
    $pass = False;
    $result = array('pass' => $pass, 'FirstName' => "", 'LastName' => ""
 , 'id' => $id);    
}
$json_response = json_encode($result);
echo $json_response;
1
  • 1
    Does the console log says anything? If you look on the Network tab of the Developer tools what content is returned from the angular request? Commented Nov 26, 2015 at 22:07

1 Answer 1

1

For ajax calls, such as this requires the header to be set as application/json, so javascript can easily read it, and also need to make error reporting to false in ajax related API designs

<?php
error_reporting(0);
...
...
...
header('Content-Type:application/json;');
echo $json_response;

this output is easily readable by javascript

Sign up to request clarification or add additional context in comments.

2 Comments

NOT when developing, no. use error_reporting(E_ALL); and when an error actually occur, fix your code. anyway, OP is probably already error_reporting(0), and don't check json_last_error() and is getting a hidden error..
your right, but my intention was if there is no error, then json is pure and easily parsed by java-script functions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.