Reputation: 35
I made this webservice that handles my database functions, and this is an AJAX call to one of the methods.
$.ajax({
type: "POST",
url: "Service/dataBaseService.asmx/getRMAData",
data: '{"RMAId": 1 }',
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
cache: false,
success: function (data) {
console.log(data);
alert(data.RMA_ID);
}
});
this is what is logged:
({d:"[{\"RMA_ID\":1,\"RequestDate\":\"2013-02-28T00:00:00\",\"Company\":1,\"Status\":\"Accepted \",\"Serial\":201764,\"LastChangeDate\":\"2013-02-28T00:00:00\",\"LastChangeBy\":\"Foreign \",\"Price\":null}]"})
However alert(data.RMA_ID)
returns undefined aswell as data.d.RMA_ID
?
How can I get hold of the values?
Upvotes: 2
Views: 6465
Reputation: 5766
Using simple javascript you need to parse JSON response
var resp = eval('(' + data + ')');
or thru jQuery
var resp = jQuery.parseJSON(data);
now you can access the data using '.' and key name
console.log(resp.d[0].RMA_ID)
Upvotes: 0
Reputation: 129832
The value of data
that you've logged is an object with a property named d
, that contains a string value. You could probably make adjustments at your server side to make the value of d
an object rather than a string, but the way it is currently constructed, you would be able to parse it into an object using JSON.parse
.
Once you've done that, the resulting object should be an array, containing one single object. Thus, your access to RMA_ID
would be as follows:
var data = JSON.parse(data.d);
alert(data[0].RMA_ID);
Upvotes: 6