Cra*_*ker 5 javascript php ajax wordpress jquery
尝试运行我的AJAX时出现500服务器错误.我是AJAX的新手.如果我在脚本中没有运行AJAX,那么每个东西都在代码中运行,例如只运行:
$("#book-appointment-form").submit();
因此,似乎所有数据库功能都很好.但是,我需要AJAX在Wordpress页面中运行我的代码.
我没有在错误日志中看到任何注释.控制台日志显示该URL指向正确的位置.我可能缺少什么?
控制台日志显示hiddenData中显示的隐藏输入中的数据:
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]
Run Code Online (Sandbox Code Playgroud)
视图:
<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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
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);
}
?>
Run Code Online (Sandbox Code Playgroud)
我曾尝试更换serializeArray()用serialize().我也尝试serializeArray()转换$.param(confirmedData)- 相同的结果真的仍然它似乎没有到达服务器.500错误仍然存在.不过我觉得serialize()可能更合适一些.
这有效:
我的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>
Run Code Online (Sandbox Code Playgroud)
我的控制器
<?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);
}
?>
Run Code Online (Sandbox Code Playgroud)
不再出现 500 服务器错误。
| 归档时间: |
|
| 查看次数: |
1024 次 |
| 最近记录: |