I'm learning the web language for creating my website and using the Laravel framework 5.4, and having trouble sending forms with ajax.
Every form I send, returns me the error:
Unprocessable Entity {"account": ["The account field is required."]}
When there are rules set up in FormRequest. I read it in several places and some said it was because it was not being sent in json, so I tried to know how to do it and tried to do it, but it still continues. I removed the rules and gave a var_dump in the result and it is returning me:
OKarray (0) {}
I have no idea where this "OK" is coming from and the reason for the inputs is not being sent. My content:
register.blade.php
@section('content')
<div class="wpb_text_column wpb_content_element">
<div class="wpb_wrapper">
<div class="title-wrapper">
<h3 class="widget-title" style="border-color: #34495e"><i class="fa fa-newspaper-o"></i> @lang('page.register.title')</h3>
<div class="clear"></div>
</div>
<div class="wcontainer">
<div class="formcontainer">
<form id="register" name="register" method="post" action="" onsubmit="$('#regresult').ajaxLoader('post', 'register', '#register'); return false;">
<table width="100%" style="border: 0">
<tr style="background: none">
<td width="50%" align="left">
<strong>@lang('page.register.account')</strong>
<input id="account" name="account" type="text" maxlength="10" tabindex="1" />
</td>
<td width="50%" align="right">
<strong>@lang('page.register.pid')</strong>
<input id="pid" name="pid" type="text" maxlength="7" tabindex="2" />
</td>
</tr>
<tr style="background: none">
<td align="left">
<strong>@lang('page.register.password')</strong>
<input id="password" name="password" type="text" maxlength="10" tabindex="3" />
</td>
<td align="right">
<strong>@lang('page.register.rpassword')</strong>
<input id="password_confirmation" name="password_confirmation" type="text" maxlength="10" tabindex="4" />
</td>
</tr>
<tr style="background: none">
<td align="left">
<strong>@lang('page.register.mail')</strong>
<input id="mail" name="mail" type="text" maxlength="30" tabindex="5" />
</td>
<td align="right">
<strong>@lang('page.register.rmail')</strong>
<input id="mail_confirmation" name="mail_confirmation" type="text" maxlength="30" tabindex="6" />
</td>
</tr>
<tr style="background: none">
<td align="left">
<div class="g-recaptcha" style="width: 302px; margin: 0 auto" data-sitekey="{{ config('recaptcha.public_key') }}" data-tabindex="7"></div>
</td>
<td align="right">
<input id="agree" name="agree" type="checkbox" tabindex="8" value="1" /> @lang('page.register.agree')<br />
<input id="subscriber" name="subscriber" type="checkbox" tabindex="9" value="1" /> @lang('page.register.subscriber')
</td>
</tr>
<tr style="background: none">
<td colspan="2" align="center" width="100%" style="text-align: center">
<button type="submit" style="width: 35%; margin: 0 auto;" tabindex="10">@lang('page.register.button')</button>
</td>
</tr>
</table>
</form>
</div>
<div id="regresult">
</div>
<div class="clear"></div>
</div>
</div>
</div>
<script type="text/javascript" src="https://www.google.com/recaptcha/api.js"></script>@stop
RegisterRequest.php
...classRegisterRequestextendsFormRequest{publicfunctionauthorize(){returntrue;}publicfunctionrules(){return[];}}
RegisterController.php
...classRegisterControllerextendsController{publicfunctioncreate(){returnview('page.register')->renderSections()['content'];}publicfunctionstore(RegisterRequest$request){var_dump($request->all());}}
web.php
//registerRoute::get('register','Page\RegisterController@create');Route::post('register','Page\RegisterController@store');
ajax.js
$.fn.extend({ajaxLoader:function(type,url,form){varself=$(this);vardata='';if(form!==undefined){data=$(form).serialize();}$.ajaxSetup({headers:{'X-CSRF-TOKEN':$('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
dataType: 'json',
contentType: 'application/json; charset=utf-8',
type: type,
url: url,
data: data,
beforeSend: function () {
self.html('<div id="loading"></div>');
},
success: function(result) {
self.html(result);
},
error: function(result) {
self.html(result.statusText + result.responseText);
}
});
}
});
I've been trying to make this work for hours and have not had it, either, a positive process.