[英]Jquery getJSON not working with Spring MVC
以下代碼無法將請求提交到服務器,因此我無法弄清原因。
這是我的jsp頁面的一部分
$(document).ready(function() {
$('#firstName').change(function() {
if(('#firstName').val().length >= 2){
$.getJSON(
"getPSPersons.html",
{ firstName: $('#firstName').val(), lastName: $('#lastName').val()},
function(data) {
buildTable(data);
}
);
}
});
});
-------------------------------
<form:form name="addperson" method="GET">
<label for="firstName">First Name</label>
<input type="text" name="firstName" id="firstName" size="15"/>
<label for="lastName">Last Name</label>
<input type="text" name="lastName" id="lastName" size="15"/>
</form:form>
和Spring控制器類的功能
@RequestMapping(value="getPSPersons.html", method = RequestMethod.GET)
public @ResponseBody List<Person> getPersonsWithNames(
@RequestParam("firstName") String firstName, @RequestParam("lastName") String lastName)
{
List<Person> personList = new ArrayList<Person>();
if(firstName.length()>=2 || lastName.length() >=2)
{
personList = personService.getPersonsWithName(firstName, lastName);
}
return personList;
}
要求是,當用戶在“名字”輸入框中輸入多個字符時,應該向服務器提交AJAx請求,以獲取所有以這些字母開頭的名字的人……但是在這里,get請求永遠不會調用此函數。我很想在JQuery請求端出現問題,但是我找不到它。
-----更新---------
在第3行上發現了錯誤..應該是if( $ ('#firstName')。val()。length> = 2){開頭的$丟失了
如果您願意而不是重新發明輪子,為什么不使用預先存在的自動建議窗口小部件-有很多用JQuery編寫的。 我只是喜歡這個: http : //code.drewwilson.com/entry/autosuggest-jquery-plugin-試試看。
我發現一個js錯誤。
('#firstName').val().length
一定是
$('#firstName').val().length
要么
$(this).val().length
您丟失了“ $”,將其修復,我發現請求已發送。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.