2016-11-05 93 views
0

我在某种程度上是一种修复。我正在尝试使用Ajax提交表单。现在,当我这样做而不创建laravel默认auth scaffold它工作正常,但如果添加auth scaffold失败。我尽我所能,但似乎无法让它提交。在laravel中使用Ajax 5.3

这里是我的代码: 控制器发送邮件 -

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('[email protected]', 'My Name')->subject('Information'); 
        }); 

        return response()->json([ 
           'responseText' => 'Mail was sent!'], 200); 
       } 
      } else { 
       return View('fail')->render(); 
      } 
     } 
} 

的路线文件:

Route::get('/', function() { 
    return view('welcome'); 
}); 

Route::post('/mail', [ 
    'as' => 'mail', 
    'uses' => '[email protected]' 
]); 

Auth::routes(); 

Route::get('/home', '[email protected]'); 

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

我的表单代码:

<div class="col-sm-6"> 
<div class="footer-content" id="myfooter"> 
    {!! Form::open(['action' => '[email protected]', '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> 

错误消息:

Failed to load resource: the server responded with a status of 401 (Unauthorized) 

回答

0

是,我是用mailgun sandbox account的问题,因此我只能当我使用laravel appauth scaffold包括与auth scaffold应用程序将允许为mailgun sandbox account不要发送邮件”不发送任何反应。

解决方案是使用常规的gmail发送它,并按预期做出响应,邮件通过。