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:

    header("Access-Control-Allow-Origin: *");
    header("Access-Control-Allow-Methods: GET, POST, OPTIONS");

The is the ajax request:

            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?


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:

        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 关于#git#的问题,请各位专家解答!(相关搜索:开源项目)
    • ¥15 matlab处理脑电数据悬赏(时序图+预处理+频谱图)
    • ¥100 r语言多元回归模型怎么看表达式
    • ¥15 PMM 9010/30P
    • ¥15 pom文件依赖管理,未找到依赖
    • ¥15 现在后端返回给我一堆下载地址,都是一个视频切片后的,如何在uniapp安卓环境下将这些分片的视频下周并且合并成原始视频并下载到本地。
    • ¥15 Unity导出2D项目运行时图片变成马赛克
    • ¥15 关于communitytoolkit.mvvm的生成器得到的代码看起来没有被使用的问题
    • ¥15 matlab中此类型的变量不支持使用点进行索引
    • ¥15 咨询第六届工业互联网数据创新大赛原始数据