2016-09-14 99 views
0

我想从我的网站调用外部API系统。在ajax响应中,我得到以下数据。如何从json响应中获取特定字段值

object(stdClass)#3 (3) { 


["status"]=> 
    string(7) "success" 
    ["code"]=> 
    int(200) 
    ["data"]=> 
    object(stdClass)#4 (3) { 

    ["desktop_url"]=> 
    string(113) "http://landing.beta.learning.social/remote?token=a_token_value" 

    ["mobile_url"]=> 
    string(115) "http://m.landing.beta.learning.social/remote?token=a_token_value" 

    } 
} 

我需要从这个response.So,我可以从field.I曾尝试用下面的代码的URL重定向页面获得desktop_url字段的值。

$.ajax({ 
    type: "POST", 
    url: path, 
    data: "email=" + $("#email_data").val() + "&fname=" + $("#fname_data").val()+"&grade="+$("#gradeID_data").val()+ 
    "&lastname="+$("#lname_data").val(), 
    success: function(message){ 
    $(message).map(function(item){console.log (item.data);}); 
    } 
}); 

这不是为我工作,给我下面的错误

Error: Syntax error, unrecognized expression: object(stdClass)#3 (3... 
+0

的响应,这不是一个有效的JSON数组。 –

回答

3

您需要的JSON编码由API

<?php 
$response = $api->call($params[,...]); 


header('content-type:application/json'); 
exit(json_encode($response)); 
+0

当我尝试在api调用中对响应进行编码时,我得到如下响应:'string(416)“”{\“status \”:\“success \”,\“code \”:200,\“ data \“:{\”token \“:\”token \“,\”desktop_url \“:\”http:\\\ \ \ \\/landing.beta.learning.social \\\/remote?token = token \“,\”mobile_url \“:\”http:\\\ \ \ \ \ \/m.landing.beta.learning.social \\\/remote?token = token \“}}”“' – Techy

+0

尝试解码响应'json_decode($ response,true);'或'$ responseStr = var_export($ response,true);' –

+0

看起来不错,你现在可以从'console.log'中获得'desktop_url' .data.desktop_url)'在你的成功功能中 – andrew