2012-04-20 75 views
5

我對JSON/Spring MVC非常新,並試圖獲得一個簡單的AJAX調用Spring MVC控制器的例子 - 但我一直返回一個錯誤400 - 錯誤請求。與JQuery/Ajax和Spring的JSON請求

經過淘汰互聯網後,我發現這通常是由於沒有設置適當的內容類型導致 - 但[我相信]我已經這樣做了。

這裏是我的AJAX調用:

//validate the object 
    var urlString = "/ajax/add/"; 

    $.ajax({ 
     type:"POST", 
     url: urlString, 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     data: {value1: 'apples', value2 : 'oranges'}, 
     success: function(result){ 
      alert("success"); 
     }, 
     error: function(jqXHR, textStatus, errorThrown){ 
      alert("error:" + textStatus + " exception:" + errorThrown); 
     } 

    }) ; 

而且我的控制器:

@Controller 
itpublic class AddController { 

private static Logger m_log = null; 

@RequestMapping(value = "/ajax/add") 
public String handle(@RequestBody String json){ 

    DummyClass dummyRequest; 
    try { 

     ObjectMapper mapper = new ObjectMapper(); 
     dummyRequest = mapper.readValue(json, DummyClass.class); 
     m_log.info("Value1: " + dummyRequest.getValue1()); 
     m_log.info("Value2: " + dummyRequest.getValue2()); 
    } catch (Exception e) { 

    } 
    finally{ 


    } 
    return "called"; 
} 

誰能幫助我嗎?

編輯

這裏是我的背景下,我用它來配置Spring:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" 
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" 
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 

<context:component-scan base-package="com.project.do" /> 

<!--<mvc:annotation-driven/> --> 

<bean class="com.project.do.interceptors.handler.mapping.HandlerInterceptorAnnotationAwareHandlerMapping "/> 

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> 

<bean class="com.project.do.viewresolver.ProjectViewResolver" > 
    <property name="tilesResolver" ref="tilesResolver" /> 
    <property name="jspResolver" ref="jspResolver" /> 
</bean> 

<bean id="tilesResolver" class ="org.springframework.web.servlet.view.UrlBasedViewResolver"> 
    <property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView" /> 
    <property name="order" value="2" /> 
</bean> 

<bean id="jspResolver" 
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="prefix" value="/WEB-INF/jsp/" /> 
    <property name="suffix" value=".jsp" /> 
    <property name="order" value="3" /> 
</bean> 

<bean id ="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer"> 
    <property name="definitions" value="/WEB-INF/tiles/tiles.xml" /> 
    <property name="preparerFactoryClass" value="org.springframework.web.servlet.view.tiles2.SpringBeanPreparerFactory"/> 
</bean> 

<mvc:resources mapping="/css/**" location="/css/" /> 
<mvc:resources mapping="/js/**" location="/js/" /> 
<mvc:resources mapping="/images/**" location="/images/" /> 
<mvc:resources mapping="/javascript/**" location="/javascript/" /> 
<mvc:resources mapping="/scripts/**" location="/scripts/" /> 
<mvc:resources mapping="/styles/**" location="/styles/" /> 
<mvc:resources mapping="/help/**" location="/help/" /> 

<!-- <mvc:view-controller path="/" view-name="welcome"/> --> 

我要指出,我有阿賈克斯工作控制器在包com.project .do.working,但帶有Ajax和JSON的非工作控制器位於同一個包中com.project.do.not.working

+0

你是如何配置Spring?你可以編輯問題並添加你的配置細節(可能是一個XML文件,除非你使用新的@Configuration風格)。 – andyb 2012-04-20 21:38:49

+0

我附加了用於配置Spring的上下文。謝謝! – kjl 2012-04-20 21:50:32

+1

您可以嘗試用@RequestParam字符串value1替換您的RequestBody,..(at)RequestParam字符串value2?你確實想在你的ReequestMapping中有方法= RequestMethod.POST(正如raddykrish所說的那樣)。 – Stealth 2012-04-21 04:30:24

回答

1

我認爲你的請求映射沒有作爲POST的RequestMethod。我猜在默認情況下,它把它作爲一個get請求,但你的AJAX是一個POST請求。嘗試改變如下

@RequestMapping(value = "/ajax/add", method = RequestMethod.POST) 
public String handle(@RequestBody String json){ 
} 
+0

我選擇這個作爲答案,因爲它似乎是必需的。事實上,我原本是這樣編碼的,但是當事情沒有奏效時就開始剝離細分。問題的大部分似乎純粹是環境問題 - 沒有人能夠幫助我解決問題。 – kjl 2012-05-29 18:42:19

+0

謝謝@kjl .... – raddykrish 2012-05-29 18:47:06

+0

@kjl,我面臨着類似的[問題](http://stackoverflow.com/questions/13075101/400-bad-request-on-spring-jquery-ajax-post) ,你能告訴我真正的問題是什麼嗎?它可以幫助我 – 2012-10-25 18:57:43

0
@Controller 
@RequestMapping("/employee/add.htm") 
public class EmployeeController { 
    @RequestMapping(method = RequestMethod.POST) 
    public @ResponseBody 
    Employee add(HttpServletRequest request, HttpServletResponse response) 
      throws Exception { 


    } 
}