一 服务器端代码

package com.cn;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
@RequestMapping("/data/inteface")
public class CSDNTest {
@RequestMapping("/getdata")
public void getSolrArticleen(String parameter,
HttpServletRequest request, HttpServletResponse response) throws Exception{
String callback = request.getParameter("jsoncallback");
parameter = new String(parameter.getBytes("iso-8859-1"), "utf-8");
//获取数据
DataRepository dataRepository = new DataRepository();
List<String> primaryItems = dataRepository.getData();
//把获取的数据转换成字符串,这里用的是FastJson,也可以根据需要用Gson
String jsonArticle = JSONObject.toJSONString(primaryItems,
SerializerFeature.DisableCircularReferenceDetect);
//把需要输出的服务器端的字符串拼接callback头
String json = callback+"(" + jsonArticle + ")";
//输出到客户端
GetOut.getWriter(json, response);
}
}

  

二 获取输出流的代码

package com.cn;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletResponse;
/**
*
* @Title: GetOut.java
*
* @Package: com.cn
*
* @Company: WiiMedia
*
* @Description: 获取输出流
*
* @author: SongJia
*
* @date: 2016-06-27 上午11:09:27
*
*/
public class GetOut {
public static PrintWriter getWriter(String msg,HttpServletResponse response) throws IOException{
response.setContentType("text/html;charset=utf-8");
PrintWriter out = null;
out = response.getWriter();
out.write(msg);
out.flush();
out.close();
return out;
}
}

  

三 客户端请求数据代码

<script type="text/javascript">
function AcquireData() {
$.ajax({
async:false,
url: "http://m.lecoonginfo.com/data/inteface/getdata, // 跨域URL
type: 'GET',
dataType: 'jsonp',
jsonp: 'jsoncallback', //默认callback
timeout: 5000,
success: function (json) {
//客户端jquery预先定义好的callback函数,
//成功获取跨域服务器上的json数据后,
//会动态执行这个callback函数
console.log(json);
}
}
});
</script>

  

JS跨域:2.解决方案之-设置回调参数的更多相关文章

  1. js跨域请求解决方案

    什么是跨域? 跨域是指一个域下的文档或脚本试图去请求另一个域下的资源,这里跨域是广义的. 广义的跨域: 1.) 资源跳转: A链接.重定向.表单提交 2.) 资源嵌入: <link>.&l ...

  2. js跨域及解决方案

    本文出自:http://www.cnblogs.com/oneword/archive/2012/12/03/2799443.html 1.什么是跨域 我们经常会在页面上使用ajax请求访问其他服务器 ...

  3. angular.js跨域post解决方案

    跨域,前端开发中经常遇到的问题,AngularJS实现跨域方式类似于Ajax,使用CORS机制. 下面阐述一下AngularJS中使用$http实现跨域请求数据. AngularJS XMLHttpR ...

  4. js跨域问题解决方案

     跨域:当协议.域名.端口号任何一个不相同时,叫称为跨域.   HTML5  CORS(cross-origin-resource-sharing)跨域资源共享: 原理:当需要访问跨域的资源时,可以通 ...

  5. js跨域问题解释 使用jsonp或jQuery的解决方案

    js跨域及解决方案 1.什么是跨域 我们经常会在页面上使用ajax请求访问其他服务器的数据,此时,客户端会出现跨域问题. 跨域问题是由于javascript语言安全限制中的同源策略造成的. 简单来说, ...

  6. Atitit.js跨域解决方案attilax大总结 后台java php c#.net的CORS支持

    Atitit.js跨域解决方案attilax大总结 后台java php c#.net的CORS支持 1. 设置 document.domain为一致  推荐1 2. Apache 反向代理 推荐1 ...

  7. js跨域请求jsonp解决方案-最简单的小demo

    这里说的js跨域是指通过js在不同的域之间进行数据传输或通信,比如用ajax向一个不同的域请求数据,或者通过js获取页面中不同域的框架中(iframe)的数据.只要协议.域名.端口有任何一个不同,都被 ...

  8. chrome浏览器的跨域设置 Google Chrome浏览器下开启禁用缓存和js跨域限制--disable-web-security

    chrome用户默认路径 Win7:C:\Users\[用户名]\AppData\Local\Google\Chrome\User Data\XP:C:\Documents and Settings\ ...

  9. vue.js及H5常见跨域问题解决方案

    一.原生H5跨域问题解决方案 1.live-server 代理解决 首先在有node.js环境下,打开命令行工具,输入 npm install live-server -g 全局安装全局安装 live ...

随机推荐

  1. C++ Primer 有感(标准库类型)

    1.当进行string对象和字符串字面值混合连接操作时,+操作符的左右操作数必须至少有一个是string类型的: string s1= "hello"; sring s2=&quo ...

  2. C# 基于密码的身份验证报错问题System.Net.NetworkCredential

    今天碰到个很奇怪的问题,在用下面这段代码调试时获取身份验证时居然报错,更奇怪的是本地VS中调试正常而在虚机上调试就报错了 ClientCredentials clientCredentials = n ...

  3. leetcode 169 Majority Element 冰山查询

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  4. Linux 学习笔记_12_文件共享服务_4_SSH

    SSH文件共享服务 一.ssh远程登录[一般的Linux系统都会默认安装并启用] 1.Linux上远程命令行登录:ssh 用户名@远程主机IP地址 常用选项: -2:表示SSH2,强制使用第二代SSH ...

  5. 【一天一道LeetCode】#60. Permutation Sequence.

    一天一道LeetCode系列 (一)题目 The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and ...

  6. 【一天一道LeetCode】#46. Permutations

    一天一道LeetCode系列 (一)题目 Given a collection of distinct numbers, return all possible permutations. For e ...

  7. java中throw与throws

    类或函数声明,用throws表示,函数体中,可能抛出的错误,后接多个. 1.系统自动抛出的异常 系统定义的编译和运行异常都可以由系统自动抛出,称为标准异常. 2.语句抛出的异常 用户程序自定义的异常和 ...

  8. Java进阶(五十一)必须记住的Myeclipse快捷键

    Java进阶(五十一)必须记住的Myeclipse快捷键 在调试程序的时候,我们经常需要注释一些代码,在用Myeclipse编程时,就可以用 Ctrl+/ 为选中的一段代码加上以 // 打头的注释:当 ...

  9. Oracle EBS订单的流程(Order->AR)

    from:http://blog.csdn.net/pan_tian/article/details/7693447 基本流程 创建订单 路径:Order Management > Orders ...

  10. 给大家推荐一个python的学习网站 http://www.codecademy.com

    注册之后,可以跟着指导,自己编码学习,比较方面: 再推荐一个在线编译的代码网站,c/c++ python  都可以: http://codepad.org