Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery.post()似乎由于某种原因被跳过_Javascript_Php_Jquery_Ajax_.post - Fatal编程技术网

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

Javascript jQuery.post()似乎由于某种原因被跳过,javascript,php,jquery,ajax,.post,Javascript,Php,Jquery,Ajax,.post,我正在试图弄清楚为什么对jQuery.post()的调用没有提取数据,或者提取后的函数没有完全运行 我有三个文件包括在内;一个HTML文件、一个JavaScript文件和一个PHP文件。HTML包含我想要的模态元素,当按下“删除”按钮时,最终会显示该元素 jQuery正在看到单击并运行$的函数 但是,当我尝试调用$.post时,根据我的chrome developer调试,它使用.post()执行了一系列处理和操作,但没有显示警报,告诉我从delete_prep.php检索的数据已准备好用于填充

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

我有三个文件包括在内;一个HTML文件、一个JavaScript文件和一个PHP文件。HTML包含我想要的模态元素,当按下“删除”按钮时,最终会显示该元素

jQuery正在看到单击并运行
$的函数

但是,当我尝试调用
$.post
时,根据我的chrome developer调试,它使用
.post()
执行了一系列处理和操作,但没有显示警报,告诉我从delete_prep.php检索的数据已准备好用于填充确认模式中的数据

我对使用任何类型的ajax都是相当陌生的,因为
.post()

我认为下面列出的代码足以检索数据,然后得到一个警告,上面写着“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>
适用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禁止。他将其移除,并解决了403问题


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

您在控制台中看到任何错误吗?你有没有试过在帖子上附加一个
fail()
回调,看看它是否被触发,如果被触发,用什么参数?我还没有考虑添加fail。但我现在肯定会尝试一下,作为补充说明,您提到您选择使用post()作为ajax的推荐替代方案。post不是ajax的替代品,它是一个包装器。post在后台使用ajax,只需将您提供的参数提供给ajax,并为您将方法类型设置为“post”。此外,正在编码的
$operator
的值到底是什么?它是使用PDO语句从我的mySQL查询中提取的关联数组,并使用
fetch(PDO::fetch_ASSOC)
<?php
require_once('obsured_path/initialize.php');

$operator = find_operator_by_id($id);
echo json_encode($operator);