![](/img/trans.png)
[英]How to get value of query string from encoded url using javascript?
[英]How do I get the value from (php encoded json) using angular javascript?
在控制台中,我只能看到成功,並且$ scope.data.username不輸出任何內容。 第二個問題:在我的PHP中,我需要發送status和status_massage嗎? 為什么?
在我的js中:
$scope.register = function register() {
$http.post('.../api/user.php', {username: $scope.user.username, password: $scope.user.password, func: 'register'}).
success(function(data, status, headers, config) {
console.log('succes');
$scope.data = data;
console.log($scope.data.username);
}).
error(function(data, status, headers, config) {
console.log('error');
});
};
在郵遞員中它將返回
{
"status": 200,
"status_message": "OK",
"data": {
"username": "smith99"
}
}
而且我的php腳本可以:
$response['status'] = 200;
$response['status_message'] = "OK";
$response['data'] = array(
'username' => $user
);
$jsonResponse = json_encode($response);
echo $jsonResponse;
目前你已經合並了status
, status_message
和data
進入你的身體反應,這被認為是data
在角的success(function(data, status, headers, config) {})
因此,快速而骯臟的修復方法是在成功回調中使用$scope.data = data.data
。
但是,這不是將所有內容合並到響應主體中的正確方法。 對於HTTP響應代碼 ,應設置為
<?php
http_response_code(200);
?>
而對於您的響應正文,您只需將username
設置為
$response['username'] = @user
$jsonResponse = json_encode($response);
echo $jsonResponse;
這樣,您無需更改當前的Angular代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.