使用Angularjs POST方法在位置0的JSON中出现意外的标记u

时间:2017-12-01 14:19:45

标签: javascript php angularjs json

在尝试使用angularjs和php进行登录时,我在JSON的位置0错误中获得了这个意外的令牌u。我不知道它意味着什么或如何解决它,这就是我正在做的事情。

.controller('LoginCtrl', function($scope,$http) {


$scope.login=function(){

var version = "1.4.0";
$http({
url: 'http://localhost/php/prueba.php',
method:'POST',
data: {
usuario: $scope.usuario,
password:$scope.password,
version:version
},
headers:{
  'Contect-Type':'application/x-www-form-urlencoded'
}
}).then(function(response){
console.log(response);
})
}

这是我的.js文件,在我的PHP中,这就是我所拥有的

<?php
//error_reporting(E_ALL);
//display_errors(1);
header('Access-Control-Allow-Origin');
header('Content-Type: application/json');


session_start();

$postdata=file_get_contents("php://input");
$request=json_decode($postdata);
$usuario = $request->usuario;
$password = $request->password;
$version = $request->version;
/*$angular_http_params = 
(array)json_decode(trim(file_get_contents('php://input')));


$usuario = $angular_http_params['usuario'];
$password = $angular_http_params['password'];
$version = $angular_http_params['version'];

*/
echo "usuario " . $usuario;
echo " password " . $password;
echo " version " . $version;
?>

所以如果有人可以帮我解决,我会很高兴的

2 个答案:

答案 0 :(得分:0)

PHP输出应该在json foramt中。除了json以外的任何东西都不应该是echo的输出。用户json_encode方法在json中构造输出。

答案 1 :(得分:0)

你应该只在PHP脚本中回显$postdata,因为它显然已经是JSON ......

要确保将哪些数据传递到您的客户端,请使用浏览器的devtoolsFiddler查看PHP响应的内容