简体   繁体   中英

How return Json data by calling an web-service through AJAX?

I have a web service which returns JSON data below code I am using for calling the web service.

 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() {} }); }; 

it return output of alert in success function as [object object] but I want it in proper json format.

You must read JSON.stringify()

use alert(JSON.stringify(data))

Example:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM