sai*_*can 2 php ajax jquery captcha laravel-4
目的:刷新验证码图像。
routes.php
Route::get('/get_captcha/{config?}', function (\Mews\Captcha\Captcha $captcha, $config = 'default') {
return $captcha->src($config);
});
Run Code Online (Sandbox Code Playgroud)
验证码图片(mypage.blade.php):
<img src="{{ Captcha::img() }}" alt="captcha" class="captcha-img" data-refresh-config="default">
Run Code Online (Sandbox Code Playgroud)
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);
});
});
});
Run Code Online (Sandbox Code Playgroud)
问题: 控制台上的js ajax错误:http://localhost/get_captcha/default 404错误。
参考:https : //github.com/mewebstudio/captcha/issues/54#issuecomment-141483501
附加信息:我在Laravel 4上使用了验证码,但是src在上面的行中找不到名为的方法$captcha->src($config)
先感谢您。
小智 6
重要说明:将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>
Run Code Online (Sandbox Code Playgroud)
在控制器内部创建一个函数,以重新生成验证码。
控制器代码
public function refereshCapcha(){
return captcha_img('flat');
}
Run Code Online (Sandbox Code Playgroud)
验证验证码请求-
'captcha' => 'required|captcha',
Run Code Online (Sandbox Code Playgroud)
将功能标记到可以使用ajax调用的路由文件中。
路由文件
Route::get('/refereshcapcha', 'HelperController@refereshCapcha');
Run Code Online (Sandbox Code Playgroud)
创建一个用于发出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>
Run Code Online (Sandbox Code Playgroud)