Reputation: 4539
Why is the code below not working? I can see in FireBug that the data
variable contains the reply with the data I expect:
"[{"nextID":"STK000022"}]"
But when I try and access the value I get "undefined".
jQuery.post( get_data_url, FormData, function(data) {
sessionStorage.ticket_id = data[0].nextID; <--- UNDEFINED HERE
})
.done(function(data) {
showPopupMsg(successClass, false, "New ticket successfully created!<br/>New ticket number: <strong>" + sessionStorage.ticket_id);
// Reset form
jQuery( '#PartnerNewTicketForm' ).reset();
})
.fail(function(jqxhr, textStatus, error ) {
var sysError = textStatus + ", " + error;
showPopupMsg(errorClass, logoutFlag, "The application has failed to create the new ticket. Error: " + sysError);
})
.always(function(data) {
});
Upvotes: 0
Views: 120
Reputation: 782693
Since the server is returning JSON, you need to tell jQuery about it, by giving a 4th argument to $.post
:
jQuery.post( get_data_url, FormData, function(data) {
sessionStorage.ticket_id = data[0].nextID;
}, 'json')
Specifying json
as the dataType
argument tells jQuery that it should parse the JSON into an object.
Upvotes: 2
Reputation: 25537
I think your server is returning string, instead of json object. So you should make it as object before use. Use jQuery.parseJSON(
to make a string as object.
jQuery.post( get_data_url, FormData, function(data) {
data=jQuery.parseJSON(data);
sessionStorage.ticket_id = data[0].nextID;
})
Upvotes: 2