简体   繁体   English

完整日历CakePhp 3.x和错误:XMLHttpRequest无法加载“…”。 请求的资源上没有'Access-Control-Allow-Origin'标头吗?

[英]Full Calendar CakePhp 3.x and Error: XMLHttpRequest cannot load '…'. No 'Access-Control-Allow-Origin' header is present on the requested resource?

I am using full calendar on cakephp 3.x and I am getting this error message only on mobile devices "XMLHttpRequest cannot load https://www.utahreia.org/events/feed ... No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' https://utahreia.org ' is therefore not allowed access." 我在cakephp 3.x上使用了完整日历,并且仅在移动设备上收到此错误消息“ XMLHttpRequest无法加载https://www.utahreia.org/events/feed ...否'Access-Control-Allow-Origin “标头出现在所请求的资源上。因此,不允许访问源' https://utahreia.org '。”

On the desktop, the events load fine, it is just the mobile devices that I get the error and I have no idea why. 在桌面上,事件加载正常,只是移动设备出现了错误,我也不知道为什么。 The feed is used to grab events from database and renders them to this url: full calendar 该提要用于从数据库中获取事件并将其呈现到以下URL: 完整日历

Here is the feed method: 这是feed方法:

public function feed($id=null) {
        $this->layout = "ajax";
        $vars = $this->request->query([]);
        $conditions = ['UNIX_TIMESTAMP(start) >=' => $vars['start'], 'UNIX_TIMESTAMP(start) <=' => $vars['end']];
        $events = $this->Events->find('all', $conditions)->contain(['EventTypes']);
        foreach($events as $event) {
            if($event->all_day === 1) {
                $allday = true;
                $end = $event->start;
            } else {
                $allday = false;
                $end = $event->end;
            }
            $event_img[] = array(
                    'id' => $event->id,
                    'title'=> $event->title,
                    'start'=> $event->start,
                    'end' => $end,
                    'allDay' => $allday,
                    'url' => Router::url('/') . 'events/view/'.$event->id,
                    'details' => $event->details,
                    'className' => $event->event_type->color
            );
        }
        $this->set("json", json_encode($event_img));
        $this->set(compact('user'));
    }

Here is the ready.js: 这是ready.js:

jQuery(document).ready(function( $ ) {

    // page is now ready, initialize the calendar...
    $('#calendar').fullCalendar({

        header: {
            left:   'title',
            center: '',
            right:  'today agendaDay,agendaWeek,month prev,next'
        },
        defaultView: 'month',
        fixedWeekCount: false,
        firstHour: 8,
        aspectRatio: 2,
        editable: adminEdit,
        events: plgFcRoot + "events/feed"
    })
});

Full Calendar page: 完整的日历页面:

<div class="page-wrap">
    <div class="Calendar index">
        <div id="calendar"></div>
    </div>
</div>

<script type="text/javascript">plgFcRoot = "https://www.utahreia.org/";</script>
<?php if (isset($current_user) && $current_user['role'] === 1): ?>
<script type="text/javascript">adminEdit = true;</script>
<?php else: ?>
<script type="text/javascript">adminEdit = false;</script>
<?php endif; ?>

<?= $this->Html->css('/full_calendar/css/fullcalendar', ['plugin' => true]); ?>
<?= $this->Html->css('/full_calendar/css/jquery.qtip.min', ['plugin' => true]); ?>
<?= $this->Html->script('/full_calendar/js/lib/jquery.min.js', ['plugin' => true]); ?>
<?= $this->Html->script('/full_calendar/js/lib/moment.min.js', ['plugin' => true]); ?>
<?= $this->Html->script('/full_calendar/js/fullcalendar.js', ['plugin' => true]); ?>
<?= $this->Html->script('/full_calendar/js/jquery.qtip.min.js', ['plugin' => true]); ?>
<?= $this->Html->script('/full_calendar/js/ready.js', ['plugin' => true]); ?>

Assumably you're running a hybrid mobile app which is why you're recieving an error on XMLHttpRequest. 假设您正在运行混合移动应用程序,这就是为什么收到XMLHttpRequest错误的原因。 The reason you're running into this error is because your mobile app is being served to the hybrid WebView locally from the mobile device, meaning that the origin of the page being served in the app doesn't match the origin of the resource you are trying to request (Your feed endpoint.) To allow your cross origin mobile app to connect to your endpoint, you're going to have to set Cross Origin Resource Sharing (CORS) headers on the request. 您遇到此错误的原因是,您的移动应用程序是从移动设备本地提供给混合WebView的,这意味着应用程序中提供的页面的来源与您所使用的资源的来源不匹配尝试请求(您的feed终结点。)要允许跨源移动应用程序连接到终结点,您将必须在请求上设置跨源资源共享(CORS)标头。 I actually wrote a plugin to do just that. 我实际上写了一个插件来做到这一点。 Here it is , I hope it helps! 在这里 ,希望对您有所帮助!

我已升级到CakePHP 3.1,并且不再遇到此问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 XMLHttpRequest无法加载。 。 。 所请求的资源上没有“ Access-Control-Allow-Origin”标头。 - XMLHttpRequest cannot load . . . No 'Access-Control-Allow-Origin' header is present on the requested resource. XMLHttpRequest无法加载http:// *********。 请求的资源上不存在“ Access-Control-Allow-Origin”标头 - XMLHttpRequest cannot load http://*********. No 'Access-Control-Allow-Origin' header is present on the requested resource XMLHttpRequest无法加载“…所请求的资源上没有&#39;Access-Control-Allow-Origin&#39;头。” — jQuery - XMLHttpRequest cannot load “ …No 'Access-Control-Allow-Origin' header is present on the requested resource.” — Jquery XMLHttpRequest无法加载…所请求的资源上没有“ Access-Control-Allow-Origin”标头 - XMLHttpRequest cannot load…No 'Access-Control-Allow-Origin' header is present on the requested resource XMLHttpRequest无法加载没有&#39;Access-Control-Allow-Origin&#39;&gt;头出现在所请求的资源上 - XMLHttpRequest cannot load No 'Access-Control-Allow-Origin' > header is present on the requested resource 仅限Chrome中的错误:XMLHttpRequest无法加载文件网址请求的资源上没有“Access-Control-Allow-Origin”标头 - Error in Chrome only: XMLHttpRequest cannot load file URL No 'Access-Control-Allow-Origin' header is present on the requested resource XMLHttpRequest请求的资源上没有“Access-Control-Allow-Origin”标头 - XMLHttpRequest No 'Access-Control-Allow-Origin' header is present on the requested resource XMLHttpRequest无法加载。 请求的资源上不存在“Access-Control-Allow-Origin”标头。 因此不允许原点访问 - XMLHttpRequest cannot load. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin is therefore not allowed access XMLHttpRequest无法加载请求的资源上不存在“Access-Control-Allow-Origin”标头。 Origin'http:// localhost:3000'Google maps - XMLHttpRequest cannot load No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' Google maps XMLHttpRequest无法加载http://example.com/myapp/jsonurl/。 请求的资源上不存在“ Access-Control-Allow-Origin”标头 - XMLHttpRequest cannot load http://example.com/myapp/jsonurl/. No 'Access-Control-Allow-Origin' header is present on the requested resource
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM