Laravel - 跨域请求 - barryvdh laravel-cors

时间:2014-10-26 12:21:57

标签: php ajax laravel-4 cross-domain

我遇到跨域请求问题。我想通过ajax从www.second-domain.com'发送get请求。到' www.first-domain.com/path/to/plugin'并返回html代码(插件视图) 我安装了' barryvdh / laravel-cors'来自Laravel 4.2的github' 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&#39; www.second-domain.com&#39;我在代码中添加了以下行。如果这条线我添加到www.first-domain.com&#39;它正在发挥作用。

<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 - &gt; 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 - &gt; 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 - &gt; 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 - &gt; 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 个答案:

答案 0 :(得分:1)

我找到了解决方案 在www.first-domain.com - &gt; www.first-domain.com请求包括标题'X-Requested-With:XMLHttpRequest',但是在www.second-domain.com上 - &gt; www.first-domain.com请求不是这个标题。
我在这个主题上找到了完整的答案Cross-Domain AJAX doesn't send X-Requested-With header