2016-05-24 79 views
0

我在使用AJAX發佈表單時遇到了麻煩。Spring MVC - AJAX表單發佈的問題

這裏是我的Ajax調用:

function submit() { 
     $.ajax({ 
      type: "POST", 
      url: "http://localhost:8080/executeRetrieve", 
      data: $("#form").serialize(), 
      dataType: "json", 
      success: function(data) { 
       alert(data); 
      } 
     }) 
    } 

這裏是我的HTML表單(他們是在同一頁):

<form id="form" method="post"> 
User <input type="text" name="user" id="user"/><br /> 
Password <input type="password" name="password" id="password"/><br /> 
<input type="submit" value="Submit" onclick="submit()"/> 

而且也,這是我的動作:

@RequestMapping(value = "/executeRetrieve", method = RequestMethod.POST, produces = "application/json; charset=utf-8") 
public @ResponseBody String executeRetrieve(HttpServletRequest request) { 
    JSONObject json = new JSONObject(); 
    json.put("message", "hello"); 

    return json.toString(); 
} 

我是困惑。不應該那樣工作?我一直在尋找至少3天的解決方案,我無法理解發生了什麼。行動方法甚至沒有達到。有人會知道我犯了什麼錯誤嗎?

在此先感謝,好朋友。

+0

有沒有網絡流量? – BevynQ

+0

@BevynQ對不起? –

+0

使用chrome,檢查元素點擊網絡。然後嘗試解僱你的活動。你應該看到一些網絡流量。特別是一個職位。 – BevynQ

回答

0

我認爲URI中存在問題,您嘗試從ajax http://localhost:8080/executeRetrieve調用。它應該包含部署在服務器中的應用程序名稱。例如http://localhost:8080/<app_name>/executeRetrieve

+0

嘗試了此操作。沒有成功:( –

+0

你能分享你的代碼,會嘗試運行嗎? –