【问题标题】:Captcha refresh (mewebstudio)验证码刷新 (mewebstudio)
【发布时间】:2016-01-22 15:05:35
【问题描述】:

用途:刷新验证码图片。

routes.php

Route::get('/get_captcha/{config?}', function (\Mews\Captcha\Captcha $captcha, $config = 'default') {
    return $captcha->src($config);
});

验证码图片(mypage.blade.php):

<img src="{{ Captcha::img() }}" alt="captcha" class="captcha-img" data-refresh-config="default">

js文件:

$(document).ready(function(){
    $('img.captcha-img').on('click', function () {
        var captcha = $(this);
        var config = captcha.data('refresh-config');
        //console.log(config);
        $.ajax({
            method: 'GET',
            url: '/get_captcha/' + config,
        }).done(function (response) {
            captcha.prop('src', response);
        });
    });
});

问题: 我的控制台上的 js ajax 错误:http://localhost/get_captcha/default 404 错误。

参考:https://github.com/mewebstudio/captcha/issues/54#issuecomment-141483501

附加信息:我正在为 Laravel 4 使用验证码,但在 $captcha-&gt;src($config) 上面的行中我找不到名为 src 的方法

提前谢谢你。

【问题讨论】:

    标签: php jquery ajax laravel-4 captcha


    【解决方案1】:

    重要提示:将 captcha_img('flat') 更改为 laavel4 语法。两者的逻辑是相同的。

    验证码 HTML-

    <div class="form-group refereshrecapcha">
    {!! captcha_img('flat') !!}
    </div>
    <input id="captcha" class="form-control" type="text" name="captcha" data-validation="required" >
    <a href="javascript:void(0)" onclick="refreshCaptcha()">Refresh</a>
    

    在控制器内创建一个重新生成验证码的函数。

    控制器代码-

    public function refereshCapcha(){
        return captcha_img('flat');
    }
    

    验证验证码请求-

    'captcha' => 'required|captcha',
    

    将您的函数标记到您可以使用 ajax 调用它的路由文件中。

    路由文件-

    Route::get('/refereshcapcha', 'HelperController@refereshCapcha');
    

    创建一个用于发出 ajax 请求的函数

    AJAX-

    <script>
    function refreshCaptcha(){
    $.ajax({
    url: "/refereshcapcha",
    type: 'get',
      dataType: 'html',        
      success: function(json) {
        $('.refereshrecapcha').html(json);
      },
      error: function(data) {
        alert('Try Again.');
      }
    });
    }
    </script>
    

    【讨论】:

      猜你喜欢
      • 2016-04-18
      • 1970-01-01
      • 2014-09-29
      • 2020-05-01
      • 2012-04-26
      • 2013-08-23
      • 2011-11-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多