同一域名下的参数传递是很容易实现的,但是跨域就存在一定的限制。当然可以搜到很多介绍的帖子,在这里我对jsonp这种单向跨域的方式进行总结,并附上一个小例子。
问题描述:
由于在服务器上开发,用不用的端口号来区分不同的项目。
实现192.168.1.10:8888可以调用8889项目上的参数。(源于单点登录,验证时的需要)
192.168.1.10:8888端:
1. controller实现:
public function test()
{
return view('test');
}
2.'test.blade.php':
<html>
<head>
<title>11111</title>
</head>
<body>
<button id="get22222">Click</button>
@if(Cookie::has('test'))
{{Cookie::get('test')}}
@endif
<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.8.0.js" type="text/javascript"></script>
<script type="text/javascript">
$("#get22222").click(function () {
$.ajax({
url: 'http://192.168.1.10:8889/test',
dataType: "jsonp",
jsonp: "callback",
success: function (data) {
alert(data);
},
error:function()
{
alert("error");
}
})
})
</script>
</body>
</html>
192.168.1.10:8889端:
1.controller实现:
public function test()
{
\Debugbar::disable();
$callback = Input::get('callback');
$arr =array('a'=>'panhe','b'=>'dada');
//return $callback."('". json_encode($arr). "')";
return response($callback."('". json_encode($arr). "')")->withCookie('test','authverify',10000000,'/')
->header('p3p', 'CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"');
}