2016-01-19 46 views
0

从PHP调用ajax时,此问题可以正常工作,但我似乎无法使用Laravel 5.2工作。我所要做的就是在提交表单时使用AJAX发送电子邮件。Ajax调用成功返回CSRF令牌并且不返回消息

这是我发送电子邮件routes.php项:

Route::post('/send', '[email protected]'); 

这是我layout.blade.php文件:

<head> 
    <meta name="csrf-token" content="{{ csrf_token() }}" /> 
</head> 

$(document).ready(function() { 
    $.ajaxSetup({ 
     headers: { 
      'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') 
     } 
    }); 

    ... 

    $("#send").button().click(function(event) { 
     var form  = $("#contact-form"), 
      postAction = form.attr('action'), 
      isValid = form.valid(), 
      valSum  = $(".validation-summary"); 

     if (isValid) { 
      var postData = { 
       message_type: "contact" 
       ,first_name: first_name.val() 
       ,last_name: last_name.val() 
       ,email: email.val() 
       ,message: message.val() 
      }; 

      // process the form 
      $.ajax({ 
       type: "POST", 
       // our data object 
       url: postAction,  // the url where we want to POST 
       data: postData 
      }) 
      // using the done promise callback 
      .done(function(data) { 
       // log data to the console so we can see 
       console.log(data); 
       // here we will handle errors and validation messages 
       if (! data.success) { 
        valSum.removeClass("success"); 
        valSum.addClass("validation-summary error"); 
        valSum.html(data); 
       } else { 
        // $(".validation-summary").html("Message sent."); 
        valSum.html(data.message); 
       } 
      }) 
      // using the fail promise callback 
      .fail(function(data) { 
       // show any errors 
       // best to remove for production 
       console.log(data); 

       valSum.removeClass("success"); 
       valSum.addClass("validation-summary error"); 
       valSum.html("Server Error: " + data.statusText + " processing your request, please contact Dorothea or report a bug."); 
      }); 
      // stop the form from submitting the normal way and refreshing the page 
      event.preventDefault(); 
     } else { 
      return false; 
     } 
    }); 

最后这里是我的CommsControllersend()功能:

public function send(Request $request) { 
    //check if its our form 
    if (Session::token() !== Request::get('csrf-token')) { 
     return Response::json(array(
      'message' => 'Unauthorized attempt to create setting' 
     )); 
    } 

    $message_type = strval(Request::input('message_type')); 
    $first_name = strval(Request::input('first_name')); 
    $last_name = strval(Request::input('last_name')); 
    $email = strval(Request::input('email')); 
    $message = strval(Request::input('message')); 

    $to = "[email protected]"; 

    // subject 
    $subject = "Dorothea - Contact Us"; 

    Mail::send('email.contact', ['request' => Request::all()], function ($message) use ($request) { 
     $message->from($this->email, $this->email); 
     $message->to($user->email, $user->email)->subject($subject); 
    }); 

    $response = array(
     'status' => 'success', 
     'message' => 'Message sent.', 
    ); 

    return Response::json($response); 
} 

这个问题与我发布here的问题有关,但不是获得MethodNotAllowedHttpException,而是从AJAX调用中返回的所有内容都是CSRF令牌值。

回答

0

.done替换为.success以获取返回数据。

.success(function(data) { 
    if (data.status != 'success') { 
     valSum.removeClass("success"); 
     valSum.addClass("validation-summary error"); 
    } 

    // eventually, print out the return message 
    valSum.html(data.message); 
}) 
+0

@ In9187请参考[这里](http://stackoverflow.com/questions/10931836/should-i-use-done-and-fail-for-new-jquery-ajax-code-instead-of-success - )为什么我使用'.done'而不是'.success'。 –

0

只想让大家试图帮助我知道如果我改变POST请求的GET请求在Laravel该邮件发送工作正常。

不太确定这是为什么,所以如果任何人可以阐明为什么这将是伟大的情况。