2013-11-14 43 views
0

當我輸入localhost:8080/ui /關於 時我調用了about.ftl頁面,並且我在裏面放了下面的代碼塊。通過SendToServlet()函數,我想給用戶的信息發送到我的控制器,這是OAuthController.javaSpring MVC Controller從來沒有從ajax中調用Post

function SendToServlet(){ 
          $.ajax({ 

             url: "localhost:8080/ui/about", 
             type: "POST",            
             data: JSON.stringify({ user:jsonObj}),          
             contentType: 'application/json', 
             success: function(result) { 
              alert(done); 
             }, 
             error: function(xhRequest, ErrorText, thrownError) { 
             alert(JSON.stringify(jsonObj)); 
             } 
            }); 
           } 
</script> 

我的Spring MVC控制器類代碼具有下列實現 - 所有它確實是,它接受用戶的信息,然後設置當前用戶:

@Controller 
@RequestMapping("/about") 
public class OAuthController { 

@RequestMapping(method = RequestMethod.POST) 
@ResponseBody 
public String post(@RequestBody String items, HttpServletRequest request, HttpServletResponse response) 
{ 




    String jsonResp = items;//sb.toString(); 
    ArrayList<String> usercredentials = new ArrayList<String>(); 
    usercredentials = parseJson(jsonResp); 
    UserMethods usermethods = new UserMethods(); 
    usermethods.setCurrentUser (usercredentials); 
    response.setContentType("text/plain"); 
    response.setCharacterEncoding("UTF-8"); 

    return "{\"success\":\"\"}"; 


} 

public ArrayList<String> parseJson(String json){ 


} 


} 

我的問題是,控制器永遠不會調用,實際上我從來沒有看到要通過Firebug的任何地方發送POST請求。我已經花了好幾天的時間,但仍然沒有運氣。你有什麼建議嗎?

+0

HTTP狀態從服務器返回。它可能是一個400錯誤的請求? – Bart

回答

0

你需要與你的函數綁定值。您的網址localhost:8080/ui/about/post僅用於定位控制器,但它不會執行任何功能,因爲您沒有對任何功能進行調用。

要調用的函數,你必須這樣做:

@RequestMapping(method = RequestMethod.POST, value ="/anyValue") 
public String anyFunctionName(anyParam){...} 

以上功能結合到URL localhost:8080/ui/about/anyValue

+0

我試過你建議用localhost更新url:8080/ui/about/control和@RequestMapping(method = RequestMethod.POST,value =「/ control」) public String post(anyParam){...},但仍然收到錯誤消息,我從來沒有看到發送的消息。 – user2008973

+0

好的。你收到什麼錯誤信息? –

+0

我的意思是我定義的函數SendToServlet返回時,函數不成功,這是jsonobject。 – user2008973

0

難道你不需要像這樣稱呼它嗎?

url: "localhost:8080/ui/about/post", 

但首先做到這一點:

@RequestMapping(method = RequestMethod.POST, value = "/post") 
+0

謝謝。我也嘗試過,但仍然通過函數獲取錯誤消息。 – user2008973

+0

什麼錯誤信息是? – Lucas

+0

每次函數返回錯誤時,我都定義變量jsonobj爲「提醒」。這就是我的意思。 – user2008973

0

如何嘗試添加.html?應用程序不會自動添加它。

+0

加入html是什麼意思?我已經有一個.ftl模板,其中有一個按鈕,當點擊該函數時,調用函數SendToServlet()。 – user2008973

相關問題