Javascript 向AJAX请求返回空字符串

Javascript 向AJAX请求返回空字符串,javascript,jquery,asp.net-mvc,ajax,Javascript,Jquery,Asp.net Mvc,Ajax,我有一个对函数的AJAX调用: $('#DeviceType').change(function () { // when the selection of the device type drop down changes // get the new value var devicetype = $(this).val(); $.ajax({ url: '@Url.Action("GetEquipmentCode")', type: 'PO

我有一个对函数的AJAX调用:

$('#DeviceType').change(function () {
   // when the selection of the device type drop down changes 
   // get the new value
   var devicetype = $(this).val();

   $.ajax({
      url: '@Url.Action("GetEquipmentCode")',
      type: 'POST',
      data: { deviceTypeID: devicetype },
      success: function (result) {
         // when the AJAX succeeds refresh the EquipmentCode text box
         $('#EquipmentCode').val(result);
      }
   });
});
功能为

[HttpPost]
public string GetEquipmentCode(int deviceTypeID)
{
   var deviceType = _db.DeviceTypes.Single(d => d.ID == deviceTypeID);

   return (deviceType.EquipmentCode != null) ? 
                      deviceType.EquipmentCode.Code : 
                      String.Empty;
}
但是,如果函数返回
String.Empty
,我在文本框中实际得到的字符串是“
[object XMLDocument]
”。如何在文本框中获取空字符串的结果?

试试看

  .... 
  data: { deviceTypeID: devicetype },
  dataType : 'html',
  ...
默认情况下,jquery尝试猜测,有时可能不准确


JSON可能是最简单的

尝试:

然后在你的行动方法中

[HttpPost]
public ActionResult GetEquipmentCode(int deviceTypeID)
{
   var deviceType = _db.DeviceTypes.Single(d => d.ID == deviceTypeID);

   return Json((deviceType.EquipmentCode != null) ? 
                      deviceType.EquipmentCode.Code : 
                      String.Empty);
}

AJAX响应应该是XML还是JSON?这是来自WCF还是MVC操作方法?MVC操作方法。还有XML或JSON-也许这是我应该返回的而不是字符串?只是将
的“text”
更改为“
html
[HttpPost]
public ActionResult GetEquipmentCode(int deviceTypeID)
{
   var deviceType = _db.DeviceTypes.Single(d => d.ID == deviceTypeID);

   return Json((deviceType.EquipmentCode != null) ? 
                      deviceType.EquipmentCode.Code : 
                      String.Empty);
}