JQuery 跨域名访问

function changeDistrict(value){

if(value == 0){
$(‘#transport_node‘).empty();
$(‘#transport_node‘).append(‘<option value="0">请选择</option>‘);
return;
}
$(‘#transport_node‘).empty();
$(‘#transport_node‘).append(‘<option value="0">加载中...</option>‘);
$.ajax({
type: ‘GET‘,
url: "http://192.168.33.114:8080/UIDTraceAdmin/transportnode/pagelist/jsonp?callbackFunction=jsonpCallback",
async: false,
dataType: "jsonp",
jsonp: "jsonpCallback",//传递给请求处理程序或页面的,用以获得jsonp回调函数名的参数名(一般默认为:callback)
success: function(o){},
timeout:3000
});
}
//注意:跨域服务端响应返回后调用此函数
function jsonpCallback(result) {
....//具体代码省略 {data:“data”} 直接用 data.data 形式读出字段值。
}

跨域服务器处理代码:

[java] view plain copy 在CODE上查看代码片派生到我的代码片
@RequestMapping("/pagelist/jsonp")
public void pagelist(@ModelAttribute TransportNode node,HttpServletRequest httpReq,
HttpSession session,HttpServletResponse response) {

//返回头部设置
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Content-type", "application/x-javascript;charset=utf-8");
response.setDateHeader("Expires", 0);

String jsonpCallback = httpReq.getParameter("callbackFunction");//jsonp回调函数名
JSONObject resultJson = new JSONObject();
PrintWriter out = null;
try {
out = response.getWriter();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
node.setRowStart((node.getPage() - 1) * node.getRows() + 1);
node.setRowEnd(node.getPage() * node.getRows());

resultJson.put("transportList", JsonUtils.toJSONList(business.getList(node)));
resultJson.put("success", true);
System.out.println(resultJson.toString());
out.println(jsonpCallback+"("+resultJson.toString()+")");//返回jsonp格式数据
out.flush();
out.close();

} catch (Exception e) {
LogWriter.log("/pagelist/jsonp",e);
try {
resultJson.put("success", false);
} catch (JSONException e1) {
e1.printStackTrace();
}
out.println(jsonpCallback+"("+resultJson.toString()+")");//返回jsonp格式数据
out.flush();
out.close();
}

时间: 2024-08-28 14:29:38

JQuery 跨域名访问的相关文章

jQuery 跨域访问问题解决方法

浏览器端跨域访问一直是个问题, 多数研发人员对待js的态度都是好了伤疤忘了疼,所以病发的时候,时不时地都要疼上一疼.记得很久以前使用iframe 加script domain 声明,yahoo js util 的方式解决二级域名跨域访问的问题. 时间过得好快,又被拉回js战场时, 跨域问题这个伤疤又开疼了. 好在,有jquery帮忙,跨域问题似乎没那么难缠了.这次也借此机会对跨域问题来给刨根问底,结合实际的开发项目,查阅了相关资料,算是解决了跨域问题..有必要记下来备忘. 跨域的安全限制都是指浏

jQuery 跨域访问的三种方式 No &#39;Access-Control-Allow-Origin&#39; header is present on the reque

问题: XMLHttpRequest cannot load http://v.xxx.com. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:63342' is therefore not allowed access. test.html:1 Resource interpreted as Script but transferred

jquery跨域访问解决方案

客户端“跨域访问”一直是一个头疼的问题,好在有jQuery帮忙,从jQuery-1.2以后跨域问题便迎刃而解.由于自己在项目中遇到跨域问题,借此机会对跨域问题来刨根问底,查阅了相关资料和自己的实践,算是解决了跨域问题.便记录下来,以供查阅.         jQuery.ajax()支持get方式的跨域,这其实是采用jsonp的方式来完成的.        真实案例:        $.ajax({             async:false,             url: 'http:

jQuery 跨域访问问题解决方法(转)

转自:http://www.jb51.net/article/21213.htm 浏览器端跨域访问一直是个问题, 多数研发人员对待js的态度都是好了伤疤忘了疼,所以病发的时候,时不时地都要疼上一疼.记得很久以前使用iframe 加script domain 声明,yahoo js util 的方式解决二级域名跨域访问的问题. 时间过得好快,又被拉回js战场时, 跨域问题这个伤疤又开疼了. 好在,有jquery帮忙,跨域问题似乎没那么难缠了.这次也借此机会对跨域问题来给刨根问底,结合实际的开发项目

利用jquery跨域访问一般处理程序

$.getJSON('http://www.taik.com?Action=getservice&r=' + Math.random() + '&jsoncallback=?', function (data) { //回调函数,处理返回的数据 jsoncallback=? 必须有 }); bool success = true; string result = ""; string msg = ""; result = BindServiceInf

java 结合jQuery实现跨域名获取数据

一.什么是跨域? 由于浏览器出于安全的考虑,采取了同源策略的限制,使得jQuery无法直接跨域名互相操作对象或数据.例如:a.com 域名下的 a.html页面利用jQuery无法操作b.com域名下b.html页面的对象或是数据,并且默认情况下也不能操作test.a.com域名下的 test.html的对象或是数据.只要满足下面条件的jQuery都会视为跨域名: 1.主域相同,子域不同,如xxx.aaa.com和yyy.aaa.com 2.域名相同,端口不同,如xxx.aaa.com:8000

Jetty Cross Origin Filter解决jQuery Ajax跨域访问的方法

当使用jQuery Ajax post请求时可能会遇到类似这样的错误提示 XMLHttpRequest cannot oad http://xxxxxx. Origin http://xxxxxx is not allowed by Access-Control-Allow-Origin. 这是Ajax跨域访问权限的问题,服务器端不接受来自另一个不同IP地址的由脚本文件发出的http请求.解决这个问题需要在服务器端进行配置使服务器端可以接受来自不同域的脚本文件的http请求.一个简单的解决方法是

用jQuery与JSONP轻松解决跨域访问的问题【转】

原文地址:http://www.jb51.net/article/46463.htm 好在,有jquery帮忙,跨域问题似乎没那么难缠了.这次也借此机会对跨域问题来给刨根问底,结合实际的开发项目,查阅了相关资料,算是解决了跨域问题..有必要记下来备忘. 跨域的安全限制都是指浏览器端来说的.服务器端是不存在跨域安全限制的,所以通过本机服务器端通过类似httpclient方式完成“跨域访问”的工作,然后在浏览器端用AJAX获取本机服务器端“跨域访问”对应的url.来间接完成跨域访问也是可以的.但很显

跨域访问Jquery实现[转]

跨域访问js实现. 环境:.net3.5+Jquery+JSON.net 因为在跨域实现,所以这里新建网站,这个网站只需要: (1) Customer类 public class Customer { public int Unid { get; set; } public string CustomerName { get; set; } public string Memo { get; set; } public string Other { get; set; } } (2)Ashx文件