Home > Backend Development > PHP Tutorial > php+jquery跨域提交问题,别嫌弃啊只有20分了

php+jquery跨域提交问题,别嫌弃啊只有20分了

WBOY
Release: 2016-06-23 14:24:52
Original
882 people have browsed it

1:A域服务器端输出json如下

$test = "{name:\"John\", message:\"hello John\"}";echo $test;
Copy after login

2:B域服务端程序如下
function sendEmail() {	var orderingId = $("#orderingId").val();	 alert(orderingId);	 $.ajax({		 type: "get",		 url:"http://www.testB.com/cronemail/cron_expressmail.php?orderingid=2222",		 cache : false, 		 dataType : "jsonp",		 jsonp: "callbackfun",		 jsonpCallback:"jsonpCallback",                 success:function (json) {			alert(json.message);		}	 })}
Copy after login

可是页面提示错误:
用户代理: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Trident/4.0; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)
时间戳: Tue, 22 Oct 2013 10:48:39 UTC


消息: Script error
行: 0
字符: 0
代码: 0
URI: http://www.test.com/cronemail/cron_expressmail.php?orderingid=2222&callbackfun=jsonpCallback&_=1382438914750


回复讨论(解决方案)

ajax 不能跨域,这是不争的事实
你的代码只是形式上的跨域,实际上是

所以该 url 必须返回合法的 js 语句
仅仅一个 json 串是不够的

$test = "{name:\"John\", message:\"hello John\"}";echo $_GET['callbackfun'].'('.$test.')';
Copy after login

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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template