2013-12-14 55 views
0

我設法讓Ajax請求到服務器有兩個參數,並從服務器字符串獲得:Document對象的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"); 
      } 
     } 
    }); 
} 

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

我在javascript中獲取response對象並驗證其值。如果它的值不是空字符串,我會收到警報消息。如果服務器通過JavaScript空字符串,我得到Document object成功操作NOT EMPTY STRING。它是什麼?

+0

你是什麼意思?你在做什麼來確定你認爲正在發生的事情? – Pointy

+0

檢查您的數據是否來自沒有html佈局的服務器。你的佈局取消在哪裏? –

回答

2

從Ajax調用的響應是一個對象,而不是字符串。要獲取您的字符串,您需要使用responseText屬性。試試這個:

if (response.responseText !== "") 

如果您正在使用jQuery,see this page瞭解更多詳情。

+0

謝謝!工作很棒! :) – IFrizy

0

成功函數傳遞的參數的類型基於發送到.ajax調用的數據類型屬性,或者從返回的數據中推斷出來。所以,你可能想嘗試明確設置數據類型:「文本」中的Ajax對象,它應該強制響應變量是一個字符串:

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

假設$是指jQuery當然... – Christophe