Home > php教程 > php手册 > body text

ajax 跨域解决方法,ajax跨域

WBOY
Release: 2016-06-13 09:29:50
Original
1187 people have browsed it

ajax 跨域解决方法,ajax跨域

最近在开发过程中,使用ajax去异步调取图片。在开发中这个功能没什么问题,可以后来提测,重新部署之后就有问题了,这就是ajax的跨域问题。

ajax本身是不支持跨域的,这是由于javascript的同源策略所导致。但是我们可以通过其他方法来解决ajax的跨域问题。

1  由于我们是利用了jquery来写的ajax,我们一开始是准备 利用jsonp来解决的,客户端类似下面写法

$.ajax({
	type : "get",
	async:false,
	url : "http://www.xxx.com/ajax.do",
	dataType : "jsonp",
	jsonp: "callbackparam",//服务端用于接收callback调用的function名的参数
	jsonpCallback:"success_jsonpCallback",//callback的function名称
	success : function(json){
		alert(json);
		alert(json[0].name);
	},
	error:function(){
		alert('fail');
	}
});
Copy after login

  服务器端写法

public void ProcessRequest (HttpContext context) {
	context.Response.ContentType = "text/plain";
	String callbackFunName = context.Request["callbackparam"];
	context.Response.Write(callbackFunName + "([ { name:\"John\"}])");
}
Copy after login

  这个方法其实蛮简单的,跟我们之前写的改动不大。

2  由于我们这次项目开发的页面比较多,改动起来涉及的地方就比较多了。最后是采取的 直接修改nginx配置实现的。平时对反向代理的理解也就是 缓存、安全、负载均衡,所以查了下方向代理

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!