您的位置:首页 > 其它

Ajax跨域请求的解决

2016-03-29 16:27 555 查看
test-client的index.jsp里面的Ajax访问test-service的Controller

Controller返回json时无法接接收,JSONP方式处理后可接收



Client:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jquery--getJSON方法实现ajax功能</title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
/**
页面加载完立即执行
*/
$(function(){
//alert("JJJ");
$.getJSON("http://127.0.0.1:8080/test-service/json?callback=?&id=123&age=22", function(data) {

alert(data.name);
});

});

</script>

<script type="text/javascript">
/**
触发执行
*/
/*$(function() {
$("#button1").click(function() {

//打开文件,并通过回调函数处理获取的数据
$.getJSON("userInfo.json", function(data) {
alert(data.name);
});
});
});*/
</script>

</head>
<body>
<div class="divTitle">
<input type="button" id="button1" value="获取数据"/>
</div>
<div id="divTip"></div>

</body>
</html>


Service:

package com.xh.controller;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class JsonController {
//@RequestMapping("/json")
@RequestMapping(value="/json"/*,produces=MediaType.APPLICATION_JSON_VALUE+";charset=utf-8"*/)
@ResponseBody
public String getJson(String id,String callback,String age )
{
System.out.println("id:"+id);
System.out.println("age:"+age);
String json="{'name':'zhangs121212andsads'}";
System.out.println(StringUtils.isEmpty(callback));
System.out.println(json );
System.out.println(callback );
if(StringUtils.isEmpty(callback))
{

return json;
}
else
{
System.out.println(callback + "(" + json + ");");
return callback + "(" + json + ");";
}

}
}


web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="taotao" version="2.5">
<display-name>tets-service</display-name>
<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

</welcome-file-list>

<!-- 初始化spring容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/ApplicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- 解决post乱码 -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- springmvc的前端控制器 -->
<servlet>
<servlet-name>tets-service</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- contextConfigLocation不是必须的, 如果不配置contextConfigLocation, springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springMVC.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>tets-service</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
springMVC.xml

<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 
<!-- 配置包扫描器 -->

<context:component-scan base-package="com.xh.controller"></context:component-scan>
<!-- 配置注解驱动 -->

<mvc:annotation-driven/>
<!-- 静态资源映射 -->
<!-- <mvc:resources location="/category.json" mapping="/category.json"/> -->

</beans>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: