jQuery.post()似乎因为某种原因被跳过了



我试图弄清楚为什么对jQuery.post()的调用没有提取数据,或者提取后的函数没有完全运行。

我有三个文件;一个HTML文件、一个JavaScript文件和一个PHP文件。HTML包含我想要的模式元素,最后按下"删除"按钮。

jQuery看到点击并运行$.on("click")的函数。

然而,根据我的chrome开发人员调试,当我尝试调用$.post时,它会对.post()进行一系列处理和操作,但不会发出警报,告诉我从delete_prep.php中检索的数据已准备好用于填充确认模式中的数据。

我对使用任何类型的ajax都很陌生,而且由于.post()显示在许多其他堆栈溢出问题上,我将其视为使用$.ajax()的推荐替代方案

我认为下面列出的代码足以检索数据,然后得到一个警报,上面写着"JSON对象"或"关联数组"或任何适用的内容。不幸的是,警报甚至没有出现。

适用的html片段

<button type="button" data-title="Delete" data-opid="<?php echo $operator['operator_id']; ?>" class="icon-btn delete">Delete</button>
<div class="modal-wrapper" id="delete_operator_modal">
<section class="modal">
<div class="modal-bar">
<button id="close_modal_button" class="close-button">&times;</button>
</div>
<div class="modal-content">
<h2>Delete Operator?</h2>
<p id="delete_operator_name">Default Message</p>
<p id="delete_operator_message">If this operator is deleted, their franchises will no longer have an
owner, and be marked 'For
Sale'.</p>
<footer class="modal-footer">
<button onclick="closeModal()" id="confirm_delete_button" class="primary button">Delete Operator</button>
<button onclick="closeModal()" id="cancel_delete_button" class="secondary button">Cancel</button>
</footer>
</div>
</section>
</div>

将为jQUERY 重写的文档脚本中

var deleteButton = document.querySelector('.icon-btn.delete');
var closeButton = document.querySelector('.close-button');
var cancelButton = document.querySelector('#cancelButton');
Modal = document.querySelector('.modal-wrapper');
function openModal() {
Modal.classList.add('open');
}
function closeModal() {
Modal.classList.remove('open');
}

适用js文件中的脚本

jQuery(function () {
// This will show the delete modal and populate it with the information from the record the last pressed button corresponds to
function showDeleteModal(id) {
// This is where the code that doesn't seem to be running begins
$.post(
'ajax_php/delete_prep.php', // Gets information for delete confirmation
{
id: id                  // Data that is used to run the SQL query
},
function (data) {
var operator = JSON.parse(data);    // Converts to an object so that it can be used as an associative array
top.alert(typeof(operator));            // DEVELOPMENT checking to make sure it is an object
}
)
;
// END NON WORKING CODE
// Show the modal once the data is changed
$('#delete_operator_modal').addClass('open');
}
$('*[data-opid]').on("click", function () {
showDeleteModal($(this).attr("data-opid"));
});
$('#close_modal_button').on("click", function () {
// call function to close the modal that corresponds to the button that was clicked
});
});

最后是delete_prep.php

<?php
require_once('obsured_path/initialize.php');
$operator = find_operator_by_id($id);
echo json_encode($operator);

聊天讨论的摘要。

发现了两个问题。首先,Tyler发现他有一个.htaccess文件,其中包含一些规则,导致请求在尝试访问时返回403 Forbidden。他删除了该规则,403问题得到了解决。

其次,他的脚本引用了一个未定义的变量。在将其修复为指向脚本中提供的$_POST['id']之后,它开始按他的意图工作。

最新更新