【问题标题】:Laravel - cross domain request - barryvdh laravel-corsLaravel - 跨域请求 - barryvdh laravel-cors
【发布时间】:2014-12-21 18:26:16
【问题描述】:

我有跨域请求的问题。我想通过 ajax 将获取请求从“www.second-domain.com”发送到“www.first-domain.com/path/to/plugin”并返回 html 代码(插件视图)。
我在“www.first-domain.com”上的 Laravel 4.2 中从 github 安装“barryvdh/laravel-cors”。

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

【问题讨论】:

  • 那么问题出在哪里? JavaScript 控制台中报告了哪些错误消息? PHP 日志中报告了哪些错误消息? HTTP 请求是否与您在浏览器开发工具的“网络”选项卡中所期望的一样?反应如何?响应是否包含允许来源标头?
  • 响应头: Cache-Control no-cache Connection keep-alive Content-Length 0 Content-Type text/html; charset=UTF-8 Date Mon, 27 Oct 2014 07:25:05 GMT Server nginx Set-Cookie laravel_session=eyJpdiI6IkdLVW14...; expires=Mon, 27-Oct-2014 09:25:05 GMT; Max-Age=7200; path=/; httponly Vary Origin X-Powered-By PHP/5.5.17 access-control-allow-origin http://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

标签: php ajax laravel-4 cross-domain


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2018-08-06
    • 2015-09-30
    • 2014-06-01
    • 2015-02-19
    • 2013-06-02
    • 2015-05-16
    • 2015-07-13
    • 2019-04-17
    • 2016-05-05
    相关资源
    最近更新 更多