0

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.

3
  • 1
    use console.log() not alert Commented Jun 28, 2017 at 7:28
  • but I want the response values for calling another webservice Commented Jun 28, 2017 at 7:29
  • 1
    That's the point. You get JSON data, don't alert it, just log it in your console. Commented Jun 28, 2017 at 7:31

1 Answer 1

1

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.
Sign up to request clarification or add additional context in comments.

6 Comments

My response is {"responses":[{"coordinate_search":{"actions":[]},"took":67,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":15500,"max_score":0.0,"hits":[]},"aggregations":{"2":{"doc_count_error_upper_bound":0,"sum_other_doc_count":0,"buckets":[{"key":263731936070,"doc_count":34},{"key":263735261942,"doc_count":26} in this I want only values present under "Key"
You mean key property inside buckets?
$.each(response.aggregations.2.bucket,function(key,value) { if(key == 'key') value.key; //Get the key value here } );
Uncaught SyntaxError: Unexpected number when I am using it like success: function (data) { numbers=JSON.stringify((data.responses.aggregations.2.buckets)); alert(numbers); },
try data.reponses[0].aggregations
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.