AJAX提交和500服务器错误 [英] AJAX submit and 500 server error
问题描述
尝试运行AJAX时出现500服务器错误。我是AJAX的新手。如果我在脚本中没有运行AJAX,那么每个东西都在代码中工作,例如只运行:
I get the 500 server error when trying to run my AJAX. I am very new to AJAX. Every thing works in the code if I run no AJAX in the script, for example just running:
$(#book-appointment-form )。submit();
因此,似乎所有数据库函数都没问题。但是,我需要AJAX在Wordpress页面中运行我的代码。
Therefore, it appears that all the database functions are fine. However, I need AJAX to run my code in a Wordpress page.
我在错误日志中看不到任何注释。控制台日志显示该URL指向正确的位置。我可能缺少什么?
I do not see any notes in the error logs. The console log shows that the url is pointing to the correct location. What may I be missing?
控制台日志显示隐藏输入中的数据显示在confirmedData中:
The console log shows data within the hidden input showing up in confirmedData:
0: Object
name: "csrfToken"
value: "0f4343dfd0e71a8fa515d08f340f7bc9"
__proto__: Object
1: Object
name: "post_data"
value: "{"customer":{"last_name":"Test","first_name":"Joe","email":"email4me@verizon.net","phone_number":"9093334444","address":"","city":"","zip_code":"","id_cellcarrier":"2","wp_id":"1"},"appointment":{"start_datetime":"2015-12-25 11:00:00","end_datetime":"2015-12-25 11:50:00","notes":"","is_unavailable":false,"id_users_provider":"85","id_services":"13"},"manage_mode":false}"
__proto__: Object
length: 2
__proto__: Array[0]
查看:
<html>
<form id="book-appointment-form" style="display:inline-block" method="post">
<button id="book-appointment-submit" type="button">Confirm</button>
<input type="hidden" name="csrfToken" />
<input type="hidden" name="post_data" />
</form>
</html>
JS
<script>
$("#book-appointment-form").submit(function(event){
var confirmedData = $(this).serializeArray();
var dataUrl = GlobalVariables.baseUrl + '/index.php/appointments/ajax_confirm_appointment';
$.post(dataUrl, confirmedData, function(response) {
////////////////////////////////////////////////////////////
console.log('Customer Confirmed Post Response:', response);
////////////////////////////////////////////////////////////
}, 'json');
event.preventDefault();
});
$("#book-appointment-form").submit();
</script>
PHP控制器
<?php
public function ajax_confirm_appointment() {
if($_POST["post_data"]){
try {
$post_data = json_decode($_POST['post_data'], true);
$appointment = $post_data['appointment'];
$customer = $post_data['customer'];
...some database stuff here ....
} catch(Exception $exc) {
$view['exceptions'][] = $exc;
}
$this->load->view('appointments/book_success', $view);
$form_data = TRUE;
break;
} else {
$form_data = FALSE;
}
echo json_encode($form_data);
}
?>
我试过替换 serializeArray()
的serialize()
。我还试过 serializeArray()
转换为 $ .param(confirmedData)
- 结果确实相同但仍然如此似乎没有到达服务器。 500错误仍然存在。我认为 serialize()
可能更合适。
I have tried replacing serializeArray()
with serialize()
. I have also tried serializeArray()
converted with $.param(confirmedData)
-- same results really and still it does not appear to reach the server. 500 error persists. I think serialize()
may be the more appropriate one however.
推荐答案
这有效:
我的JS
<script>
$("#book-appointment-form").submit(function(event){
var postData = {
csrfToken: $('input[name=csrfToken]').val(),
post_data: jQuery.parseJSON($('input[name="post_data"]').val())
};
var postUrl = GlobalVariables.baseUrl + '/index.php/appointments/ajax_confirm_appointment';
$.post(postUrl, postData, function(response) {
////////////////////////////////////////////////////////////
console.log('Customer Confirmed Post Response:', response);
////////////////////////////////////////////////////////////
if (!GeneralFunctions.handleAjaxExceptions(response)) return;
}, 'json');
});
</script>
我的控制器
<?php
public function ajax_confirm_appointment() {
try {
$post_data = $_POST['post_data'];
$appointment = $post_data['appointment'];
$customer = $post_data['customer'];
...some database stuff here ....
}
echo json_encode($yn_response);
}
?>
不再有500个服务器错误。
No more 500 server error.
这篇关于AJAX提交和500服务器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!