Ajax跨域请求的解决
来源:互联网 发布:手机如何网络翻墙 编辑:程序博客网 时间:2024/06/16 13:50
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;@Controllerpublic class JsonController {//@RequestMapping("/json")@RequestMapping(value="/json"/*,produces=MediaType.APPLICATION_JSON_VALUE+";charset=utf-8"*/)@ResponseBodypublic 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>
0 0
- ajax跨域请求的解决方式
- Ajax跨域请求的解决
- 解决同域Nginx ajax 请求跨域的问题
- 通过jsonp解决ajax的跨域请求问题
- Tomcat下 解决Ajax请求跨域的问题
- 解决ajax请求跨域的两个方法
- 解决JQuery的Ajax请求跨域问题
- 通过jsonp解决ajax的跨域请求问题
- 通过jsonp解决ajax的跨域请求问题
- 完美解决ajax跨域请求的问题
- 通过jsonp解决ajax的跨域请求问题
- 关于ajax post请求跨域问题的解决心得
- springMVC3和springMVC4解决ajax跨域请求的问题
- Nginx解决ajax跨域请求问题
- 解决ajax跨域请求浏览器兼容问题
- 使用jsonp解决ajax跨域请求
- 用jsonp解决ajax跨域请求
- 解决ajax跨域请求问题
- jeecms图片链接
- OpenGL名词解释笔记
- POI操作Excel小结
- flex 左侧树形菜单,打击左侧菜单 右侧添加一个NavigatorContent
- cvFindContours 提取外轮廓
- Ajax跨域请求的解决
- ASP.NET使用Quartz.NET实现定时任务调度
- 执行索引的前提条件
- 数据库处理千万级数据的解决方案
- mysql_fetch_row
- CocoaPods详解之----制作篇
- Android性能分析-启动速度优化
- python变量的作用域
- Ajax异步上传文件