Marcin
Marcin

Reputation: 83

Laravel - cross domain request - barryvdh laravel-cors

I have problem with cross domain request. I want to send get request via ajax from 'www.second-domain.com' to 'www.first-domain.com/path/to/plugin' and return html code (plugin view).
I install 'barryvdh/laravel-cors' from github in Laravel 4.2 on '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>

On 'www.second-domain.com' I added the following line to the code. If this line I added to 'www.first-domain.com' it is working.

<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,
    )
)

EDIT: Request headers (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

Response headers (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

Request headers (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

Response headers (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

Upvotes: 1

Views: 2142

Answers (1)

Marcin
Marcin

Reputation: 83

I found the solution.
In www.first-domain.com -> www.first-domain.com request was include the header 'X-Requested-With: XMLHttpRequest', but on www.second-domain.com -> www.first-domain.com request wasn't this header.
The full answer I found on this topic Cross-Domain AJAX doesn't send X-Requested-With header

Upvotes: 1

Related Questions