【问题标题】:jQuery.post() Seemingly being skipped for one reason or anotherjQuery.post() 似乎由于某种原因被跳过
【发布时间】:2018-07-24 21:23:26
【问题描述】:

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

我包含三个文件; HTML 文件、JavaScript 文件和 PHP 文件。 HTML 包含我希望最终按下“删除”按钮的模式元素。

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

但是,当我尝试调用 $.post 时,根据我的 chrome 开发人员调试,它使用 .post() 进行了一系列处理和操作,但没有弹出我的警报告诉我正在检索的数据来自我的 delete_prep.php 已准备好用于填充确认模式内的数据。

我对使用任何类型的 ajax 还是很陌生,因为 .post() 显示在许多其他堆栈溢出问题中,我认为这是使用 $.ajax() 的推荐替代方法

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

适用的html sn-ps

<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);

【问题讨论】:

  • 您在控制台中看到任何错误吗?您是否尝试在帖子上附加fail() 回调以查看它是否被触发,如果是,则使用什么参数?
  • 我还没有考虑添加失败。但我现在肯定会尝试的
  • 另外,作为旁注,您提到您选择使用 post() 作为 ajax 的推荐替代方法。 post 不是 ajax 的替代品,它是一个包装器。 post 在后台使用 ajax 并简单地将您提供的参数提供给 ajax,并为您将方法类型设置为“POST”。
  • 另外,正在编码的$operator 的值究竟是什么?
  • 它是我的 mySQL 查询中使用 PDO 语句的关联数组,并使用 fetch(PDO::FETCH_ASSOC) 放入该数组中

标签: javascript php jquery ajax .post


【解决方案1】:

聊天讨论摘要。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-07
    • 1970-01-01
    • 1970-01-01
    • 2013-08-20
    • 2017-03-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多