helloxielan 2014-10-07 12:26 采纳率: 0%
浏览 39

HTTPS ajax请求

I recently switched my site to use HTTPS. I am using Laravel as a php framework. At the beginning I had an issue where I would get a 'Origin null is not allowed by Access-Control-Allow-Origin.' error, but then I had fixed that by using this in my filters.php:

App::before(function($request)
{
    header("Access-Control-Allow-Origin: *");
    header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
});

The is the ajax request:

    $.ajax({
            type: 'GET',
            url: '/media/search/',
            data: { 'q':search_string },
            success: function(d) { }
    });

Now instead of the Cross Origin error I am getting a redirect to my login page instead of the content that should be loaded. I think it has something to do with the CSRF token implementation that Laravel has, but I am not sure of the route I should take to either add that token to the request or how I could fix it. Anyone have any suggestions?

EDIT:

If I don't have those header functions in my filter.php I get this error:

XMLHttpRequest cannot load http://website.com/media/search?&q=500. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://website.com' is therefore not allowed access.

After this I had proceeded to use this call instead:

$.ajax({
        type: 'GET',
        url: 'https://website.com/media/search/',
        data: { 'q':search_string },
        success: function(d) { }
});

but still no luck

  • 写回答

1条回答 默认 最新

  • weixin_33701294 2014-10-07 12:34
    关注

    If it's related to CRSF you could remove AJAX from it:

    Route::filter('csrf', function()
    {
        if (!Request::ajax() && Session::token() != Input::get('_token'))
        {
            throw new Illuminate\Session\TokenMismatchException;
        }
    });
    

    adding !Request::ajax() rule at the beginning. You could also add _token to your data.

    You should also look at your app/config/session.php file :

    'secure' => false,
    

    if it is set to true and you make AJAX request from http it might not work

    评论

报告相同问题?

悬赏问题

  • ¥15 前台报错,加我微信帮我解决一下,15043457399
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题