sprig之使用ajax来实现向后台实现json类型数据的传递

首先要说的是一定要引对jar包,不是单独json类型的包,而是json包和spring的依赖jar包都要引入。这里给一个maven地址,可以实现对所有的jar包都进行引入操作。(本人就是因为jar包没引对而一直出现415错误,比较坑)


<!--进行json依赖的jar报-->
  	<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
			<version>2.9.2</version>
	</dependency>

接收下来是全部代码:web.xml就不显示了。没有什么新添加的东西

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:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
	<mvc:annotation-driven></mvc:annotation-driven>
	<!--静态资源解析器,用于实现对js代码进行直接访问-->
	<mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
	<mvc:annotation-driven></mvc:annotation-driven>
	<context:component-scan base-package="com.controller"></context:component-scan>
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/view/"/>
		<property name="suffix" value=".jsp"></property>
	</bean>

</beans>

后台:

package com.controller;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.model.UserInfo;

@Controller
@RequestMapping(value="loginController")
public class LoginController {
	@ResponseBody
	@RequestMapping(value="method1")
	public String method1(@RequestBody String username)
	{
		System.out.println("访问method1");
		System.out.println(username);
		return username;
	}
	
	@ResponseBody
	@RequestMapping(value="method2")
	public UserInfo method2(@RequestBody UserInfo  userinfo)
	{
		System.out.println("访问method2");
		System.out.println(userinfo);
		return userinfo;
	}
	
	@ResponseBody
	@RequestMapping("method3")
	public List<UserInfo> method3(@RequestBody List<UserInfo> userinfos)
	{
		System.out.println("userinfos="+userinfos);
		for (Object object : userinfos) {
			System.out.println(object);
		}
		return userinfos;
	}
}

页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
	function sendAjax1()
	{
		/*创建一个js对象*/
		var username='qingzhiyu';
		/*将js对象转变为json类型的字符串数据对象*/
		var username=JSON.stringify(username); 
		$.ajax
		({
			url:"loginController/method1",
			type:"post",
			/*进行json字符串数据对象的提交*/
			data:username,
			contentType:"application/json",
			success:function(data)
			{
				alert(data);
			},
			error:function()
			{
				alert("程序出错");
			}
		});
	}
	/*实现json对象的提交*/
	function sendAjax2()
	{
		/*将一个json字符串对象传递到后台之后解析为一个对象进行接收*/
		$.ajax
		({
			url:"loginController/method2",
			type:"post",
			/*进行json字符串数据对象的提交*/
			data:'{"username":"qingzhiyu","password":"00000"}',
			contentType:"application/json",
			success:function(data)
			{
				alert(data);
			},
			error:function()
			{
				alert("程序出错");
			}
		});
	}
	/*实现json数组的提交*/
	function sendAjax3()
	{
		/*实现以数组对象的形式来实现向后台当中进行json数据对象的传值操作*/
		var userInfo1={'username':'qingzhiyu1','password':'并州1'};
		var userInfo2={'username':'qingzhiyu2','password':'并州2'};
		var userInfo3={'username':'qingzhiyu3','password':'并州3'};
		var userInfos=[];//建立一个数组对象
		userInfos.push(userInfo1);
		userInfos.push(userInfo2);
		userInfos.push(userInfo3);
		/*将一个json字符串对象传递到后台之后解析为一个对象进行接收*/
		var userInfosJson = JSON.stringify(userInfos);
		$.ajax
		({
			url:"loginController/method3",
			type:"post",
			cache:false,
			/*进行json字符串数据对象的提交*/
			data:userInfosJson,
			contentType:"application/json;charset=utf-8",
			success:function(data)
			{
				alert(data);
			},
			error:function()
			{
				alert("程序出错");
			}
		});
	}
</script>
</head>
<body>
<!--使用异步端提交的方式来实现向后台当中传递一个json字符串对象,然后将对该json字符串对象进行解析为一个json对象-->
	<input type="button" onclick="sendAjax1()" value="button">
	<input type="button" onclick="sendAjax2()" value="button">
	<input type="button" onclick="sendAjax3()" value="button">
</body>
</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值