tags:

views:

84

answers:

2

Hello,

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?

+3  A: 

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

You can write

alert(response.d[0].StatusCode)
SLaks
A: 

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.

The Elite Gentleman