将AJAX字符串数据添加到隐藏字段

时间:2016-06-29 08:30:41

标签: jquery ajax

我的ajax代码如下:

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;
    }
});

这会返回类似的数据

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

如何将此值分配给customercode

我已经尝试customercode = (data.d);,但这仍然不起作用

1 个答案:

答案 0 :(得分:2)

由于您正在使用jQuery,请将xml - 响应包装在jQuery对象中并在其上调用find以获取相应的节点:

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