Php 从RESTAPI提取JSON响应
My API返回以下JSON响应:Php 从RESTAPI提取JSON响应,php,json,ionic-framework,jwt,Php,Json,Ionic Framework,Jwt,My API返回以下JSON响应: [{"result":{"status":1,"message":"Token generated successfully","stoken":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImp0aSI6IjVmMXoyaTNhbDJ4eCJ9.eyJpc3MiOiJodHRwOlwvXC9lc2FsZXMuY29tIiwiYXVkIjoiZVNhbGVzIiwianRpIjoiNWYxejJpM2FsMnh4IiwiaWF0I
[{"result":{"status":1,"message":"Token generated successfully","stoken":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImp0aSI6IjVmMXoyaTNhbDJ4eCJ9.eyJpc3MiOiJodHRwOlwvXC9lc2FsZXMuY29tIiwiYXVkIjoiZVNhbGVzIiwianRpIjoiNWYxejJpM2FsMnh4IiwiaWF0IjoxNDczODMwNDExLCJuYmYiOjE0NzM4MzA0NzEsImV4cCI6MTQ3MzgzNDAxMSwidWlkIjoiYWRtaW4iLCJ1Z3JwIjoic2FsZXMifQ.eeFU68UdAIkZuWtSK8H0mfJRsGM0aaCdZ2MJX4ZQUF0"}}]
我在爱奥尼亚的代码:
.controller('loginCtrl', ['$scope', '$http', function ($scope, $http ) {
$scope.data = {};
$scope.submit = function(){
var link = 'http://myapi.com/jwt/auth/';
$http.post(link, {username : $scope.data.username, password : $scope.data.password})
.success(function(data, status, headers,config){
$scope.response = data; // for UI
var jsParse = JSON.parse(data)
//how to retrieve the 'stoken' value from the JSON ?
})
.error(function(data, status, headers,config){
console.log('data error');
})
.then(function (res){
$scope.response = res.data;
});
};
}])
生成json的PHP代码:
$responses[] = array('result'=>
array( 'status' => 1,
'message' => 'Token generated successfully',
'stoken' => ''.$token,
)
);
//return json_encode(['result' => 1, 'message' => 'Token generated successfully', 'token' => '' . $token,]);
return json_encode($responses);
$http.post(链接,{username:$scope.data.username,password:$scope.data.password})
.success(函数(数据、状态、标题、配置){
$scope.response=数据;
$scope.stoken=$scope.response[0].result.stoken;
})
.error(函数(数据、状态、标题、配置){
console.log(“数据错误”);
})
.然后(功能(res){
$scope.response=res.data;
});代码>在JS中,您可以通过索引访问:jsParse['stoken']
json返回在>>[{\'result\':{\'status\':1,\'message\':\'Token generated successfully\',\'stoken\':\“EYJ0Exiaioijkv1qilJJJJ0HbGcIjIjIjUzi1NiijVmxOyatnHbJ4ECJ9.EYJpC9Lc2FszMuy29TiiwyxVkijoizNbgVziiwIjjIjJJm2FmH4IiIiw0IjOxNdQ4OyYYYYOYOYOYOYOYOYNZJ4NZJ4NZJ9.EjJJJ9IjJJJJ9IjIjIjIjIjIjIjIjIjIjIjIjIjIjIjIjIjIjIjIjIjIjIjIjIjIjIjIjIjIjIjIjIjIjIjIjIjI(var-jsParse=JSON.Parse(data);
已经是JSON了。因此,您只需执行var-token=data['stoken];
。对不起,我误导了您。从控制台>>>$scope.response[0]。结果未定义。我已将我的PHP代码添加到问题中。函数成功(response){$scope.stoken response=response.data[0]。result.stoken;}
试试这个。