Purpose : to refresh the captcha image.

routes.php

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

captcha image (mypage.blade.php):

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

js file:

$(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);
        });
    });
});

Issue : js ajax error on my console : http://localhost/get_captcha/default 404 error.

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

Additional information : I'm using captcha for Laravel 4, and i wasn't able to find a method called src, in the line above $captcha->src($config)

Thank you in advance.

shareimprove this question
 

1 Answer 正确答案

Important Note: Change the captcha_img('flat') to laavel4 syntax. The logic is same for both.

Captcha 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>

Create a function inside a controller that regenerate the captcha code.

Controller Code-

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

Validate Captcha Request-

'captcha' => 'required|captcha',

Mark your function into the route file that can you call it using ajax.

Route File-

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

Create a function for make a ajax request

AJAX-

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

来自  https://stackoverflow.com/questions/33296221/captcha-refresh-mewebstudio