Brendan Gooden Brendan Gooden - 5 months ago 13
jQuery Question

Add AJAX string data to hidden field

My ajax code is as follows:

var customername = $('#SelectCustomer').val();
var customercode;

$.ajax({
type: "POST",
url: "OrderFormServices.asmx/GetCustomerCode",
data: { 'customername': customername},
dataType: "json",
cache: false,
success: function (data) {
customercode = data;
$('#hiddenCustomerCode').value = customercode;
}
});


This returns data like so

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">CUST-009012</string>


How can I assign this value to
customercode
??

I've tried
customercode = (data.d);
but this still doesn't work

Answer

Since you're using jQuery, wrap the xml-response inside a jQuery object and call find on it to get the corresponding node:

$.ajax({ 
    type: "POST",
    url: "OrderFormServices.asmx/GetCustomerCode",
    data: { 'customername': customername},
    dataType: "xml",
    cache: false,
    success: function (data) {            
         $customercode= $(data).find("string");
         $('#hiddenCustomerCode').val($customercode);
    }
});
Comments