无法获取Wordpress Ajax表单来提交

时间:2019-05-24 18:47:16

标签: ajax wordpress

我正在将应用程序移植到WordPress。它使用表格通过复选框和下拉菜单选择客户在成人家庭住宅中要寻找的属性。它在每个onchange和keyup上重新搜索数据库。最初,我在PHP中具有独立的应用程序,但是当我将其迁移到WordPress时,我开始遇到问题。

当前在WP中,我对代码进行了条件化($ DavesWay == 1)来执行普通的no-WordPress-方式的ajax和($ DavesWay == 0)来进行WordPress-方式的操作。

在非WordPress方式中,Ajax可以正常工作,除了我在搜索表单和Ajax放入数据的result-div之间获得了WP标头和菜单。我没有从WP或JS中得到任何错误安慰。在WP-way中,显示了搜索表单,但是当我选中任何复选框时,什么也没有发生。 JS控制台显示

POST http://localhost/demo4/wp-admin/admin-ajax.php 400(错误请求)

但是我看不出有什么办法可以准确地说明它在抱怨什么。我应该如何解决这个问题?

故障排除=检查HTML输出,PHP代码中的大量回显和退出,并查看JS控制台。

function submitPg1(theForm) {
        // Used with onChange from "most" form elements, but not on those that change the page
        // rather than the select criteria. Such as rowsPerPage, pageNumber etc.
        setById("pageNo", "1"); // set inital page
        mySubmit();
    }

    function mySubmit(theForm) { // The actual ajax submit
        // DO NOT set page number
            jQuery.ajax({ // create an AJAX call...
            data: jQuery("#asi_search_form").serialize(),       // get the form data
            type: jQuery("#asi_search_form").attr("method"),    // GET or POST
            url:  jQuery("#asi_search_form").attr("action"),    // the file to call
            success: function (response) {                      // on success..
                jQuery("#result").html(response);               // update the DIV
            }
        })
    }

    function setById(id, value) { // Used to set vales by Id
        x = document.getElementById(id).value;
        x.value = value;
    }

    // 1st submit with blank selection
    jQuery(document).ready(function () { submitPg1(this.form) });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
Code fragments: (from the displayed page source)

<div id="asi_container" class="asi_container" >
        <noscript><h2>This site requires Javascript and cookies to function. See the Help page for how to enable them.</h2></noscript>
        <div id="searchForm">
            <form id="asi_search_form" name="asi_search_form" method="post"  action="http://localhost/demo4/wp-admin/admin-ajax.php"> 
                <input type="hidden" name="action" value="asi_LTCfetchAll_ajax" style="display: none; visibility: hidden; opacity: 0;">
                        <table id="greenTable" class="asi_table" title="The Green areas are for site administration, not typical users">
                        <tbody>

PHP code:

$DavesWay = 0; 
if ($DavesWay == 1){  //echo "Daves Way Setup"; // Dave's way, which works but prints the menu twice
    if( $operation == "submit"){ 
        require("asi_LTCfetchAll.php"); // for each onchange or onkeyup
    }else{
        add_filter( 'the_content', 'asi_tc_admin', '12' ); // Initial page refresh # must be >12
    }
}else{
    // The WordPress way that I could't get to work -- asi_LTCfetch never gets called 
    function asi_LTCfetchAll_ajax(){
        //echo "<br /> Goto to Submit function";   // DEBUG
        require($asi_plugin_dir . "/includes" . '/asi_LTCfetchAll.php');
    }

    add_action( "wp_ajax_asi_LTCfetchAll_ajax", "asi_LTCfetchAll_ajax" );          // admin users
    add_action( "wp_ajax_nopriv_asi_LTCfetchAll_ajax", "asi_LTCfetchAll_ajax" );   // non-logged in users
    add_filter( "the_content", "asi_tc_admin", "12" );                      // Initial page refresh # must be >12
}

1 个答案:

答案 0 :(得分:0)

尝试将JavaScript更改为their documentation中WordPress推荐的方式:

public virtual ObjectResult<AdminOrder> GetAdminOrders(int orderStatus)
{
  return ((IObjectContextAdapter) this).ObjectContext.ExecuteStoreQuery<AdminOrder>("AdminContracts", new ObjectParameter("@StatusId", orderStatus));
}