【转】$.getJSON与PHP跨域请求问题解决
今天写了个数据接口供前端同事使用,本来很简单的事,没想到遇到一些问题,在此记录一下。后端:php提供json数据接口;前端:利用$.getJSON获取数据处理。
错误如下:
在火狐浏览器中:SyntaxError: missing ; before statement
经搜索解决方案得到如下信息:
$b = json_encode($data);
header("Access-Control-Allow-Origin:http:xxx.cn");//(1)
echo "{$_GET['jsoncallback']}({$b})";//(2)
exit;
如果在PHP中少了(1)行的代码,则会出现
XMLHttpRequest cannot load ''. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' ' is therefore not allowed access.
如果少了第(2)行代码
在谷歌浏览器中:Uncaught SyntaxError: Unexpected token :
在火狐浏览器中:SyntaxError: missing ; before statement
参考文章:http://blog.163.com/lvshutao@126/blog/static/164637467201442253942499/
页:
[1]