1

I am using JQuery to execute an operation within a web service. After writing data back to my databaes, the service returns a JSON response. My request looks like the following:

$.ajax({
  url: "/services/myService.svc/PostMessage",
  type: "POST",
  contentType: "application/json; charset=utf-8",
  data: '{"message":"testing","comments":"test"}',
  dataType: "json",
  success: function (response) {
    if ((response.d != null) && (response.d.length > 0)) {
       // Parse the status code here
    }
    else { alert("error!"); }
  },
  error: function (req, msg, obj) {
    alert("error: " + req.responseText);
  }
});

When my response is returned, response.d contains the following:

[{"StatusCode":1}]

How do I parse out the value of the StatusCode?

1
  • 1
    Is response.d a string or an array? Commented Mar 24, 2010 at 14:22

2 Answers 2

3

This is an array containing an object with a StatusCode property.

You can write

alert(response.d[0].StatusCode)
Sign up to request clarification or add additional context in comments.

Comments

0

If your function returns an array of d objects you can do this:

if ((response.d != null) && (response.d.length > 0)) {
       // Parse the status code here
       for (var i = 0; i < response.d.length; i++) {
          alert(response.d[i].StatusCode);
        }
    }

Hope this helps.

Comments

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.