Laravel — кросс-доменный запрос — barryvdh laravel-cors

У меня проблема с кросс-доменным запросом. Я хочу отправить запрос на получение через ajax с «www.second-domain.com» на «www.first-domain.com/path/to/plugin» и вернуть html-код (вид плагина).
Я устанавливаю «barryvdh / laravel-cors» из github в Laravel 4.2 на «www.first-domain.com».

plugin.blade.php

<div id="bookingPlugin">
<div class="container">
<div class="bookingPlugin">
<div class="bookingPluginHeader">
<div class="bookingPluginTxtCenter">REZERWACJA ON-LINE</div>
</div>
<div class="bookingPluginContent bookingPluginNoPadding">
@foreach ($categories as $category)
<div class="bookingPluginButtonContainer">
<button id="{{{ $category->CategoryId }}}" class="bookingPluginTxtCenter">{{{ $category->CategoryName }}}</button>
</div>
@endforeach
</div>
</div>
</div>
</div>
<div id="bookingWindow"></div>
<script>
$.getScript('http://plugins.first-domain.com/bookingWindow.js');
$.getScript('http://first-domain.com/packages/jquery-ui/jquery-ui.min.js');
$("head").append(
"<link href=\"http://first-domain.com/packages/jquery-ui/jquery-ui.min.css\" rel=\"stylesheet\" media=\"all\" type=\"text/css\">");
$("#bookingPlugin button").click(function(){
openBookingWindow("{{ $hotelId }}", this.id);
});
</script>

На «www.second-domain.com» я добавил следующую строку в код. Если эту строку я добавил на «www.first-domain.com», она работает.

<script type="text/javascript" src="http://plugins.first-domain.com/book.js"></script>

book.js

$("head").append(
"<style>"+
"#bookingPlugin{position:absolute;top:200px;width:100%;z-index:999;}"+
"#bookingPlugin button{font-size:100%;margin:0;vertical-align:baseline;line-height:normal;text-transform:uppercase;background:#2670b5;}"+
"#bookingPlugin button{width:100%;cursor:pointer;color:#fff;-webkit-appearance:button;border:1px solid #fff;outline:0;padding:5px;}"+
"#bookingPlugin button:hover{background:#275DA2}"+
".bookingPlugin{width:250px;background-color:#fff;color:#444;border:1px solid #fff;padding:5px}"+
".bookingPluginNoPadding{padding:0;}"+
".bookingPluginHeader{width:100%;font-weight:bold;border-bottom:1px dotted #444;margin:0 0 5px 0;padding:5px;}"+
".bookingPluginTxtCenter{text-align:center;}"+
".bookingPluginContent{width:100%;}"+
".bookingPluginButtonContainer{width:100%;}"+
"</style>");

$.ajax({
//send get ajax request to laravel
type:'get',
//call to route
url:'http://www.first-domain.com/path/to/plugin',
//return data type as html
dataType:'html'
}).done(function(data){
//insert returned data into body element
$("body").append(data);
}).fail(function(jqXHR, ajaxOptions, thrownError){
alert(thrownError);
});

barryvdh / Laravel-CORS / config.php

'defaults' => array(
'supportsCredentials' => false,
'allowedOrigins' => array(),
'allowedHeaders' => array(),
'allowedMethods' => array(),
'exposedHeaders' => array(),
'maxAge' => 0,
'hosts' => array(),
),

'paths' => array(
'^/' => array(
'allowedOrigins' => array('*'),
'allowedHeaders' => array('Content-Type'),
'allowedMethods' => array('POST', 'PUT', 'GET', 'DELETE', 'OPTIONS'),
'maxAge' => 3600,
)
)

РЕДАКТИРОВАТЬ:
Заголовки запроса (www.second-domain.com -> www.first-domain.com)

Accept  text/html, */*; q=0.01
Accept-Encoding gzip, deflate
Accept-Language pl,en-US;q=0.7,en;q=0.3
DNT 1
Host    first-domain.com
Origin  http://www.first-domain.com
Referer http://www.first-domain.com/
User-Agent  Mozilla/5.0 (Windows NT 6.1; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0

Заголовки ответа (www.second-domain.com -> www.first-domain.com)

Cache-Control   no-cache
Connection  keep-alive
Content-Length  0
Content-Type    text/html; charset=UTF-8
Date    Mon, 27 Oct 2014 07:49:01 GMT
Server  nginx
Set-Cookie  laravel_session=eyJpdiI6Imtva3...; expires=Mon, 27-Oct-2014 09:49:01 GMT; Max-Age=7200; path=/; httponly
Vary    Origin
X-Powered-By    PHP/5.5.17
access-control-allow-origin http://www.first-domain.com

Заголовки запроса (www.first-domain.com -> www.first-domain.com)

Accept  text/html, */*; q=0.01
Accept-Encoding gzip, deflate
Accept-Language pl,en-US;q=0.7,en;q=0.3
Cookie  laravel_session=eyJpdiI6IjB1V...; _ga=GA1.2.1119242176.1414394349; _gat=1
DNT 1
Host    first-domain.com
Referer http://first-domain.com/
User-Agent  Mozilla/5.0 (Windows NT 6.1; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0
X-Requested-With    XMLHttpRequest

Заголовки ответа (www.first-domain.com -> www.first-domain.com)

Cache-Control   no-cache
Connection  keep-alive
Content-Encoding    gzip
Content-Type    text/html; charset=UTF-8
Date    Mon, 27 Oct 2014 07:48:32 GMT
Server  nginx
Set-Cookie  laravel_session=eyJpdiI6ImVxd...; expires=Mon, 27-Oct-2014 09:48:32 GMT; Max-Age=7200; path=/; httponly
Transfer-Encoding   chunked
Vary    Accept-Encoding
X-Powered-By    PHP/5.5.17

1

Решение

Я нашел решение.
В www.first-domain.com -> www.first-domain.com запрос был включен заголовок «X-Requested-With: XMLHttpRequest», но на www.second-domain.com -> www.first-domain.com запрос не был этот заголовок.
Полный ответ я нашел по этой теме Междоменный AJAX не отправляет заголовок X-Requested-With

1

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector