带有Spring的JQuery / Ajax的JSON请求

我是JSON / Spring MVC的新手,正在尝试获取对Spring MVC控制器进行AJAX调用的简单示例-但我一直返回400错误-错误的请求。

搜寻Internet后,我发现这通常是由于未设置适当的内容类型引起的-但是[我相信]我已经做到了。

这是我的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的Controller,但是不工作的Ajax和JSON控制器在同一个com.project.do.not.working包中

回答:

我认为您的请求映射不具有RequestMethod作为POST。我猜默认情况下它接受一个get请求,但是您的AJAX是一个POST请求。尝试如下更改

@RequestMapping(value = "/ajax/add", method = RequestMethod.POST)

public String handle(@RequestBody String json){

}

以上是 带有Spring的JQuery / Ajax的JSON请求 的全部内容, 来源链接: utcz.com/qa/406434.html

回到顶部