[英]Ajax use in laravel 5.3
I am in a bit of I a fix. 我有点解决。 I am trying to submit a form using Ajax.
我正在尝试使用Ajax提交表单。 Now when I do this without creating the
laravel
default auth scaffold
it works fine, but if add the auth scaffold
it fails. 现在,当我不创建
laravel
默认auth scaffold
而执行此操作时,它可以正常工作,但是如果添加auth scaffold
它将失败。 I have tried all I can but can't seem to get it to submit. 我已尽我所能,但似乎无法将其提交。
Here is my code: controller to send mail -- 这是我的代码:发送邮件的控制器-
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request;
class MailController extends Controller
{
//
public function index(Request $request)
{
if ($request->ajax()) {
$validator = Validator::make($request->all(), [
'first_name' => 'required',
'last_name' => 'required',
'email' => 'required|email',
'mymessage' => 'required',
'g-recaptcha-response' => 'required|captcha',
]);
if ($validator->fails()) {
return redirect()->back()
->withErrors($validator)
->withInput();
} else {
// get input fields values
$data = [
'firstName' => $request->input('first_name'),
'lastName' => $request->input('last_name'),
'email' => $request->input('email'),
'mymessage' => $request->input('mymessage'),
];
Mail::send('emails.email', $data, function ($message) {
official email
$message->to('xxxxxxxxx@gmail.com', 'My Name')->subject('Information');
});
return response()->json([
'responseText' => 'Mail was sent!'], 200);
}
} else {
return View('fail')->render();
}
}
}
The Route files: 路线文件:
Route::get('/', function () {
return view('welcome');
});
Route::post('/mail', [
'as' => 'mail',
'uses' => 'MailController@index'
]);
Auth::routes();
Route::get('/home', 'HomeController@index');
Ajax code: Ajax代码:
$(document).ready(function() {
var options = {
beforeSubmit: validate,
url: '/mail',
data: $(this).serialize(),
type: 'POST',
dataType: 'json',
clearForm: true,
success: great,
error: lost
};
$('#footer-form').ajaxForm(options);
});
function validate(formData, jgForm, options) {
for (var i = 0; i < formData.length; i++) {
if (!formData[i].value) {
alert('Please enter a value for all fields');
return false;
}
}
}
function great(responseText, statusText, formData) {
// prevent multiple form submission
$('#mail_btn').prop('disabled', true);
// show alert on success
$(".alert-success").prop("hidden", false);
// remove mail error information if displayed
$(".alert-info").prop("hidden", true);
// reset google recaptcha
grecaptcha.reset();
}
function lost(formData) {
// prevent multiple form submission
$('#mail_btn').prop('disabled', false);
$(".alert-info").prop("hidden", false);
}
My form code: 我的表格代码:
<div class="col-sm-6">
<div class="footer-content" id="myfooter">
{!! Form::open(['action' => 'MailController@index', 'method' => 'post', 'role' => 'form', 'id' => 'footer-form']) !!}
<div class="form-group has-feedback">
{!! Form::label('first_name', null, ['class' => 'sr-only']) !!}
{!! Form::text('first_name', null, ['class' => 'form-control', 'placeholder' => 'First Name']) !!}
<i class="fa fa-user form-control-feedback"></i>
@if($errors->has('first_name'))
{{ $errors->first('first_name') }}
@endif
</div>
<div class="form-group has-feedback">
{!! Form::label('last_name', null, ['class' => 'sr-only']) !!}
{!! Form::text('last_name', null, ['class' => 'form-control', 'placeholder' => 'Last Name']) !!}
<i class="fa fa-user form-control-feedback"></i>
@if($errors->has('last_name'))
{{ $errors->first('last_name') }}
@endif
</div>
<div class="form-group has-feedback">
{!! Form::label('email', null, ['class' => 'sr-only']) !!}
{!! Form::email('email', null, ['class' => 'form-control', 'placeholder' => 'Email address']) !!}
<i class="fa fa-envelope form-control-feedback"></i>
@if($errors->has('email'))
{{ $errors->first('email') }}
@endif
</div>
<div class="form-group has-feedback">
{!! Form::label('mymessage', null, ['class' => 'sr-only']) !!}
{!! Form::textarea('mymessage', null, ['class' => 'form-control', 'rows' => 8, 'cols' => 3, 'placeholder' => 'Message']) !!}
<i class="fa fa-pencil form-control-feedback"></i>
@if($errors->has('mymessage'))
{{ $errors->first('mymessage') }}
@endif
</div>
<div class="form-group has-feedback">
{!! app('captcha')->display() !!}
</div>
{!! Form::submit('Send', ['class' => 'btn btn-default', 'id' => 'mail_btn']) !!}
{!! Form::close() !!}
<div class="alert alert-success" id="mail_alert" role="alert" hidden>
Mail Sent!
</div>
<div class="alert alert-info" id="mail_info" role="alert" hidden>
Mail Sending Error!
</div>
</div>
</div>
Error message: 错误信息:
Failed to load resource: the server responded with a status of 401 (Unauthorized)
The problem was that I was using mailgun sandbox account
hence I was only able to send mail when I use the laravel app
with the auth scaffold
include with the auth scaffold
the app would allow as mailgun sandbox account
don't send any reponse. 问题是,我使用
mailgun sandbox account
,因此我只能当我使用发送邮件laravel app
与auth scaffold
包括与auth scaffold
的应用程序将允许作为mailgun sandbox account
不发送任何效应初探。
Solution was to use regular gmail
send it and it responded as expected and the mail went through. 解决方案是使用常规
gmail
发送,它按预期方式响应,邮件通过。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.