这个问题在从PHP调用ajax时工作正常,但我似乎无法使用Laravel 5.2。我要做的就是在提交表单时使用AJAX发送电子邮件。
这是我发送电子邮件的routes.php
条目:
Route::post('/send', 'CommsController@send');
这是我的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;
}
});
最后这是我的CommsController
send()
函数:
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 = "robinson.jeanine6@gmail.com";
// 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有关,但是现在从AJAX调用返回的所有内容都不是MethodNotAllowedHttpException
,而是获得CSRF令牌值。
答案 0 :(得分: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);
})
答案 1 :(得分:0)
只是想让每个试图帮助我的人都知道,如果我将发布请求更改为获取请求,那么在Laravel中发送电子邮件的工作正常。
不太清楚为什么会这样,所以如果有人能够解释为什么会出现这种情况会很棒。