2017-06-28 22 views
0

我有一个Web服务,它返回JSON数据,下面我用来调用Web服务的代码。如何通过AJAX调用Web服务来返回Json数据?

jQuery.ajax({ 
 
    url: 'http://localhost:5606/xyz', 
 
    type: "POST", 
 
    contentType: "application/json; charset=utf-8", 
 
    dataType: 'json', 
 
    data: '{"a":"b"}', 
 
    success: function(responses, textStatus, XMLHttpRequest) { 
 
    alert(responses); 
 
    }, 
 
    error: function(xhr, err) { 
 
    console.log("readyState: " + xhr.readyState + "\nstatus: " + xhr.status); 
 
    console.log("responseText: " + xhr.responseText); 
 
    }, 
 
    complete: function() {} 
 
}); 
 
};

其返回警报输出成功的功能像[目标对象]但我想它在正确的JSON格式。

+1

使用'的console.log()''不alert' – guradio

+0

但我想响应值调用另一个webservice –

+1

这就是要点。您可以获取JSON数据,不要“提醒”它,只需将其记录在控制台中即可。 –

回答

1

您必须阅读JSON.stringify()

使用alert(JSON.stringify(data))

例子:

var response = {}; 

response.status ="success"; 
response.data="Your data"; 

alert(response); //It will give you [object object] 
console.log(response); //Gives JSON data in console 
alert(JSON.stringify(response)); //Alerts json string 

if(response.status == "success") 
    //Pass response.data to the next webservice it will still be in the json format. 
+0

我的回答是'{“responses”:[{“coordinate_search”:{“actions”:[]},“took”:67,“timed_out”:false,“_ shards”:{“total”:5, “:5,” 失败 “:0},” 命中 “:{” 总 “:15500”,MAX_SCORE “:0.0,” 命中 “:[]},” 聚合 “:{” 2 “:{” doc_count_error_upper_bound“: 0,“sum_other_doc_count”:0,“桶”:[{“key”:263731936070,“doc_count”:34},{“key”:263735261942,“doc_count”:26}' 在这里我只想要值“钥匙” –

+0

您是指桶内的关键属性? –

+0

$。每个(response.aggregations.2.bucket,功能(键,值) { 如果(键== '键') value.key; //这里获取键值 } ); –

相关问题