jquery - 使用 Spring 进行 JQuery/Ajax 的 JSON 请求

标签 jquery ajax json spring-mvc

我对 JSON/Spring MVC 非常陌生,并且正在尝试获取对 Spring MVC Controller 的 AJAX 调用的简单示例 - 但我不断返回 400 错误 - 错误请求。

在互联网上搜索后,我发现这通常是由于没有设置适当的内容类型引起的 - 但[我相信]我已经这样做了。

这是我的 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 :

@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 的非工作 Controller 位于同一个包 com.project.do.not.working 中

最佳答案

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

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

关于jquery - 使用 Spring 进行 JQuery/Ajax 的 JSON 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10253209/

相关文章:

json - 通过合并字段使用 MailChimp API 3.0 搜索成员

javascript - 当嵌套函数仍在运行时暂停外部函数

javascript - 在 jquery 中声明一个 if 语句来显示日期

javascript - jQuery - 允许插入文本的组合框

jquery - ajax 调用中 session 过期时回发

ajax - 导轨3 : Is there a best practice for updating attributes without a form?

php - 通过 Ajax 加载 PHP 时出现 fatal error

jquery - 使用带有 CSS 的 Knockout JS 在类 bg 颜色之间淡入淡出

javascript - AJAX和PHP登录失败

javascript - 如何在异步方法中返回Json对象以查看