Spring JQuery/Ajax 进行 Json请求
来源:互联网 发布:网络配眼镜 编辑:程序博客网 时间:2024/04/30 11:43
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.
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); } }) ;
And my 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"; }
Can anyone help me out?
edit
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"/> -->
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
Spring框架介绍
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){ }
- Spring JQuery/Ajax 进行 Json请求
- struts2 + jquery + json 进行ajax请求
- struts2 + jquery + json 进行ajax请求
- struts2 + jquery + json 进行ajax请求
- struts2 + jquery + json 进行ajax请求
- struts2 + jquery + json 进行ajax请求
- struts2 + jquery + json 进行ajax请求
- struts2 + jquery + json 进行ajax请求 .
- struts2 + jquery + json 进行ajax请求
- struts2 + jquery + json 进行ajax请求 (struts2)
- struts2 + jquery + json 进行ajax请求
- struts2学习之struts2 + jquery + json进行AJAX请求
- struts2 在jquery 进行ajax请求 返回json 和String
- Spring MVC Controller与jquery ajax请求处理json
- Spring MVC Controller与jquery ajax请求处理json
- jquery Ajax请求本地json
- spring mvc json ajax jquery
- 使用jQuery ajax请求json数据
- 关于ffmpeg 的总结(一个linux 下 集 屏幕录像录音,音频视频转换,合并音频视频文件,格式转换于一身的命
- linux 目录下文件应该怎样放
- linux里输入dbca时,出现exception in thread main
- How to Change .NET Configuration Files at Runtime (including for WCF)
- 一个经典实用的iptables shell脚本
- Spring JQuery/Ajax 进行 Json请求
- FFMPEG转码后得到的MP4必须要加载完才能播放的问题
- ARM定义特殊寄存器(*(volatile unsigned long *))的理解
- windows server 2008 R2/windows 7管理十驱动器映射及本地账户管理
- 解决Android的ListView控件滚动时背景变黑(转)
- c++: string 的函数replace()用法
- Android - LayoutInflater
- 2011世界计算机专业排名
- 开源软件技术