希望这是一个简单的,并且如标题中所提到的,我设置的javascript枚举在服务器上是空的。这是一个剪掉的淡化代码,您可以尝试:
routes.php文件
Route::post('generalenquiries/send', 'CommsController@send');
layout.blade.php
<head>
<meta name="csrf-token" content="{{ csrf_token() }}" />
...
</head>
javascript代码
$(document).ready(function() {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
var EmailTypeEnum = {
GENERAL_ENQUIRY: 0
,SELF_CATERING: 1
,REPORT_BUG: 2
,NEWSLETTER: 3
,properties: {
0: {name: "GENERAL_ENQUIRY", value: 0, code: "GE"},
1: {name: "SELF_CATERING", value: 1, code: "SC"},
2: {name: "REPORT_BUG", value: 2, code: "RB"},
3: {name: "NEWSLETTER", value: 3, code: "N"}
}
};
$( "#general_enquiries_form" ).submit(function() {
var form = $( this ),
action = form.attr("action"),
isValid = form.valid(),
valSum = $(".validation-summary");
var emailType = EmailTypeEnum.GENERAL_ENQUIRY;
var emailTypeName = EmailTypeEnum.properties[emailType].name; // emailTypeName == "GENERAL_ENQUIRY"
if ( isValid ) {
valSum.removeClass( "error" );
valSum.addClass( "success" );
valSum.html( "Sending message..." );
var data = {
message_type: emailTypeName
,first_name: first_name.val()
,last_name: last_name.val()
};
$.ajax({
type: "POST",
// our data object
url: action, // the url where we want to POST
data: data
})
// 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 != true ) {
valSum.removeClass( "success" );
valSum.addClass( "error" );
valSum.html( data.message );
} else {
valSum.removeClass( "error" );
valSum.addClass( "success" );
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." );
});
}
});
});
CommsController.php
class CommsController extends Controller {
public function send(Request $request) {
try {
// $request->input('message_type') returns empty but $request->input('first_name') displays the correct value.
echo ($request->input('message_type') ." ". $request->input('first_name'));
die();
}
catch(Exception $e) {
$response = array (
'success' => false,
'message' => $e->getMessage(),
$request
);
}
}
}