本文介绍了JSON请求与jQuery /阿贾克斯同春的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新的JSON / Spring MVC和想获得一个AJAX调用一个Spring MVC的控制器的一个简单的例子 - 但是我回头率400错误 ​​- 错误的请求。

I'm very new to JSON/Spring MVC and am trying to get a simple example of an AJAX call to a Spring MVC controller - but I keep returning an error of 400 - Bad Request.

在网上淘之后,我发现,这通常是由不设置适当的内容类型造成的 - 但[我相信]我已经做到了。

After scouring the Internet, I've found that this is usually caused by not setting the appropriate content type - but [I believe] I have done that.

下面是我的AJAX调用:

Here is my AJAX call:

    //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:

Here is my context that I use to configure 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

I should note that I have a working Controller with Ajax in the package com.project.do.working, but the nonworking Controller with Ajax and JSON is in the same package com.project.do.not.working

推荐答案

我觉得你的要求映射不具有RequestMethod为POST。我想在默认情况下它把它的GET请求,但您的AJAX是一个POST请求。尝试改变如下

I think your request mapping is not having the RequestMethod as POST. I guess by default it taking it a get request but your AJAX is a POST request. Try changing as below

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

这篇关于JSON请求与jQuery /阿贾克斯同春的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 03:47