我正在學習Ajax並試圖讓它在我的項目上工作。基本上我試圖實施谷歌喜歡項目的建議。
我有發送Ajax請求到服務器異步和得到相關的建議使用Spring MVC在Ajax響應中獲取406錯誤
function showHint(str) {
if (str.length == 0) {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
\t console.log(str);
\t var xmlhttp = new XMLHttpRequest();
\t console.log(xmlhttp.readyState);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
}
};
xmlhttp.open("GET", "/hint?word=" + str, true);
xmlhttp.send();
}
}
<label>Name <input id="peak" type="text" name="peak_name" onkeyup="showHint(this.value)">
\t \t \t \t \t \t \t <p id="peak"></p>
\t \t \t \t \t \t <p>Suggestions: <span id="txtHint"></span></p>
的Spring MVC的控制器
@RequestMapping(value = { "/hint" }, method = RequestMethod.GET,params={"word"})
public @ResponseBody ArrayList<String> hint(ModelMap model,@RequestParam String word) {
System.out.println("Inside Hint");
String[] hints = { "Ram", "Shyam" };
ArrayList<String> returnhint = new ArrayList<String>();
// lookup all hints from array if $q is different from ""
if (word != null) {
word = word.toLowerCase();
int length = returnhint.size();
for (int j = 0; j < length; j++) {
if (word.contains(hints[j])) {
returnhint.add(hints[j]);
}
}
}
return returnhint;
}
我是新來的Ajax的UI。那麼是否有具體的方法xmlhttp對象的responseText必須處理?
我的問題是,因爲我已經在響應正文中傳遞ArrayList,那麼如何獲得ajax來獲取該響應並相應地更新UI?
檢查這個答案是否可以接受你http://stackoverflow.com/questions/5908466/jquery-spring-mvc-requestbody-and-json-making-it-work-together/5908632#5908632 –
我的問題是因爲我已經在響應主體中傳遞了ArrayList,那麼如何獲得ajax來獲得該響應並相應地更新UI? – Sohil