简体   繁体   English

ajax响应JavaScript中的文档对象

[英]Document object in ajax response JavaScript

I try make ajax request to server with two parameters and get from server string: 我尝试使用两个参数向服务器发出ajax请求,并从服务器字符串获取:

JavaScript: JavaScript的:

function getLetterOfResponsibilityNote(selectedCountryCode, selectedIntendedUseType) {
    $.ajax({
        type: "POST",
        url: "/Admin/Applications/SelectLetterOfRespinsibilityNote",
        cache: false,
        data: { countryCode: selectedCountryCode, intendedUseType: selectedIntendedUseType },
        success: function(response) {
            if (response !== "") {
                alert("1");
            }
        }
    });
}

And mvc action: 和mvc行动:

[HttpPost]
public string SelectLetterOfRespinsibilityNote(string countryCode, string intendedUseType)
{
  var countryDetails = new List<ContryLetterOfResponsibility>
  {
    new ContryLetterOfResponsibility
  {
    CountryCode = countryCode,
      IntendedUseType = intendedUseType
  }
};

string xml = XmlSerializerUtil(countryDetails);
var country = _countryService.GetLetterOfResponsibilityNotesByCountryCodeList(xml).FirstOrDefault();

if (country != null)
{
  return country.LetterOfResponsibilityNote;
}

return string.Empty;
}

I get response object in javascript and verify its value. 我在javascript中获取response对象并验证其值。 If its value not empty string, I get alert message. 如果它的值不是空字符串,我得到警报消息。 If server pass in JavaScript empty string, i get Document object in success action NOT EMPTY STRING. 如果服务器传入JavaScript空字符串,我将获得Document object成功动作NOT EMPTY STRING。 What is it? 它是什么?

The response from an ajax call is an object, not a string. 来自ajax调用的响应是一个对象,而不是一个字符串。 To get your string, you need to use the responseText property. 要获取字符串,需要使用responseText属性。 Try this: 尝试这个:

if (response.responseText !== "")

If you are using jQuery, see this page for more details. 如果您使用的是jQuery, 请参阅此页面以获取更多详细信息。

The success function is passed an argument whose type is either based on the datatype property sent to the .ajax call, or is inferred from the returned data. success函数传递一个参数,该类型的类型基于发送到.ajax调用的数据类型属性,或者是从返回的数据推断出来的。 So, you may want to try explicitly setting datatype: "text" in the ajax object, which should force the response variable to be a string: 因此,您可能希望尝试在ajax对象中显式设置数据类型:“text”,这应该强制响应变量为字符串:

function getLetterOfResponsibilityNote(selectedCountryCode, selectedIntendedUseType) {
    $.ajax({
        datatype: "text", // <-- added
        type: "POST",
        url: "/Admin/Applications/SelectLetterOfRespinsibilityNote",
        cache: false,
        data: { countryCode: selectedCountryCode, intendedUseType: selectedIntendedUseType },
        success: function(response) {
            if (response !== "") {
                alert("1");
            }
        }
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM