jQuery.post() 似乎由于某种原因被跳过

jQuery.post() Seemingly being skipped for one reason or another

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

我包含了三个文件;一个 HTML 文件、一个 JavaScript 文件和一个 PHP 文件。 HTML 包含我希望最终通过按下 "delete" 按钮调出的模式元素。

jQuery 正在查看 $.on("click") 的点击和 运行 功能。

然而,当我尝试调用 $.post 时,根据我的 chrome 开发人员调试,它用 .post() 做了一堆处理和操作,但没有调出我的提醒我从 delete_prep.php 中检索的数据已准备好用于填充确认模式中的数据。

我对使用任何类型的 ajax 都很陌生,并且由于 .post() 出现在许多其他堆栈溢出问题中,我将其视为使用 [=20= 的推荐替代方法]

我认为下面列出的代码足以检索数据,然后得到一个提示 "JSON object" 或 "associative array" 或任何适用的提示。不幸的是,警报甚至没有出现。

适用的 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 文件,其中包含一些规则,导致请求 return 在尝试访问它时出现 403 Forbidden。他删除了它并解决了 403。

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