2013-02-20 80 views
0

我正在嘗試使用Jquery Ajax對我的Spring MVC應用程序進行Ajax調用。應用程序控制器工作正常,但我無法讓這個Ajax測試控制器工作。該警報被觸發,但是對控制器的調用從不做出。我也嘗試過使用load,get,post。他們都沒有打電話給服務器。這讓我覺得我正在做一些明顯錯誤的事情。如果我將URL直接放在瀏覽器地址欄上,則會調用控制器。Spring MVC + JQuery + Ajax問題

如果有人能指導我正確的方向或告訴我我做錯了什麼,我會很感激。

的JavaScript

<html> 
<head> 
<script type="text/javascript" src="jquery-1.8.1.min.js"> </script> 
<script type="text/javascript"> 
    function doAjax() { 
     alert("did it even get here?"); 
     $.ajax({ 
      url : "simpleRequestTest.do", 
      method: "GET",   
      success : function(response) { 
       $('#show').html(response); 
      } 
     }); 
    } 
</script> 
</head> 
<body> 
    <h1>Simple Test </h1> 
    <a href="javascript:doAjax();"> Simple Test </a> 
    <br /> 
    <div id="show">...</div> 
</body> 
</html> 

控制器

@RequestMapping("/simpleRequestTest") 
public @ResponseBody String performSimple() { 
    return "Very Simple Test"; 
} 
+3

提示:使用開發者工具在瀏覽器中觀看網絡流量。你可以看到究竟哪個URL被打了,服務器返回了什麼。這很可能會爲您提供所需的信息。另外,你是否試過簡單地將瀏覽器指向simpleRequestTest.do?怎麼了? – StriplingWarrior 2013-02-20 05:25:15

+0

你正在請求'simpleRequestTest.do',但你的控制器映射是'/ simpleRequestTest'沒有'.do'。我認爲服務器扔404未找到錯誤 – 2013-02-20 05:33:03

+0

我認爲你的ajax網址是不正確的。 – Jason 2013-02-20 05:44:45

回答

1

你可以檢查你是否使用正確的路徑來包含jquery-1.8.1.min.js。

我檢查了它的工作文件代碼爲我。

1

我認爲你是在ajax通話

失蹤dataType試試這個

function doAjax() { 
     alert("did it even get here?"); 
     $.ajax({ 
      url : "simpleRequestTest.do", 
      method: "GET",   
      success : function(response) { 
       $('#show').html(response); 
      }, 
      dataType: 'text' 
     }); 
    } 
1

只要改變url : "simpleRequestTest.do",url : "simpleRequestTest",method: "GET",到​​

我認爲方法中的jQuery 1.5版被刪除,最新