0

I try make ajax request to server with two parameters and get from server string:

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:

[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. 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. What is it?

2
  • What do you mean? What are you doing to determine the thing that you think is happening? Commented Dec 14, 2013 at 16:45
  • Check if you data responsed from server without html-layout. Where your layout cancelling? Commented Dec 14, 2013 at 16:46

2 Answers 2

2

The response from an ajax call is an object, not a string. To get your string, you need to use the responseText property. Try this:

if (response.responseText !== "")

If you are using jQuery, see this page for more details.

Sign up to request clarification or add additional context in comments.

Comments

0

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. So, you may want to try explicitly setting datatype: "text" in the ajax object, which should force the response variable to be a string:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.