Ajax not updating to database

时间:2016-11-12 作者:sleepy_daze

因此,我在获取用户输入以发布到我的数据库中时遇到了一些困难。

每当我尝试更新表时,我都能够在前端成功地完成它,但在刷新时,数据更改不会持续。检查数据库进一步验证数据是否未更改。。。似乎“成功”正在传入AJAX,但出于某种原因,它并没有更新数据库。

事实上,对于我的HTTP请求,Chrome告诉我,出于某种原因,我的POST请求正在作为GET请求处理,我不知道为什么。。。

JS:

    jQuery(document).ready(function($){
  $(".edit_tr").click(function () {
    var commitID = $(this).attr(\'id\');
    // on click, hide the text
    $("#name_"+commitID).hide();
    $("#created_"+commitID).hide();
    $("#status_"+commitID).hide();
    $("#disbanded_"+commitID).hide();

    // on click, show the input fields for editing
    $("#name_input_"+commitID).show();
    $("#created_input_"+commitID).show();
    $("#status_input_"+commitID).show();
    $("#disbanded_input_"+commitID).show();
  }).change(function() {
    var commitID = $(this).attr(\'id\');
    var name = $("#name_input_"+commitID).val();
    var date_created = $("#created_input_"+commitID).val();
    var status = $("#status_input_"+commitID).val();
    var disbanded = $("#disbanded_input_"+commitID).val();
    var dataString = {
     id: commitID,
     name: name,
     date_created: date_created,
     status: status,
     disbanded: disbanded
    };

    console.log(dataString);
    // can place loading image here

    jQuery.post({
      url: "/wp-admin/admin-ajax.php",
      data: dataString,
      action: \'editCommittee\',
      error: function(xhr, status, error) {
        var err = eval("(" + xhr.responseText + ")");
        alert(err.Message);
      },
      success: function (response) {
        $("#name_"+commitID).html(name);
        $("#created_"+commitID).html(date_created);
        $("#status_"+commitID).html(status);
        $("#disbanded_"+commitID).html(disbanded);
        console.log("Got this from server: " + response);
        console.log("This is what happened to data: ");
        console.dir(dataString);
      }
    });
});
functions.php:

add_action(\'wp_ajax_editCommittee\', \'editCommittee\');

add_action(\'wp_ajax_nopriv_editCommittee\', \'editCommittee\');

function editCommittee() {
  global $wpdb;

    if($_POST[\'id\']) {
      $id = esc_sql($_POST[\'id\']);
      $name = esc_sql($_POST[\'name\']);
      $created = esc_sql($_POST[\'date_created\']);
      $status = esc_sql($_POST[\'status\']);
      $disbanded = esc_sql($_POST[\'disbanded\']);

      $wpdb->update(\'wp_committees\',
                    array(
                        \'name\' => $name
                    ),

                    array(
                        \'committee_id\' => $id
                    ),

                    array(
                      \'%s\'
                    )
      );
      exit;
    }
}
我前端的数据正在打印到表格中,所以我没有表格。。。对此有任何见解都会很有帮助!

Edit:

var_dump($_REQUEST); 在输出表中数据的页面上给出array(0){}12次--与我在表中打印的项目数相同。

var_dump($_REQUEST); 在…上functions.php 不打印任何内容

console.log(response) 在…上JS 文件打印0.

不管我把var_dump($_REQUEST) 在if语句中(之前$wpdb->update) 或者在if语句之前,不会打印任何内容。

Edit 2:

function getAjax() {
  wp_register_script(\'ajax_url_script\', get_template_directory_uri() . \'/js/edit.js\', array(\'jQuery\'));
  wp_localize_script( \'ajax_url_script\', \'postAjax\', array(\'ajaxurl\' => admin_url( \'admin-ajax.php\' )));
  wp_enqueue_script( \'ajax_url_script\');
}
add_action( \'wp_enqueue_scripts\', \'getAjax\' );

1 个回复
最合适的回答,由SO网友:jgraup 整理而成

测试AJAX是否正常工作

add_action( \'wp_ajax_editCommittee\', \'test_ajax_request\' );
add_action( \'wp_ajax_nopriv_editCommittee\', \'test_ajax_request\' );
function test_ajax_request() {

    echo "Request: " . PHP_EOL;

    var_dump( $_REQUEST );

    exit;
}
并更新您的请求以移动action 进入data 发送时间:

jQuery (document).ready (function($) {


    $ (".edit_tr").click (function() {


        var commitID = $ (this).attr (\'id\');

        // on click, hide the text
        $ ("#name_" + commitID).hide ();
        $ ("#created_" + commitID).hide ();
        $ ("#status_" + commitID).hide ();
        $ ("#disbanded_" + commitID).hide ();

        // on click, show the input fields for editing
        $ ("#name_input_" + commitID).show ();
        $ ("#created_input_" + commitID).show ();
        $ ("#status_input_" + commitID).show ();
        $ ("#disbanded_input_" + commitID).show ();

    }).change (function() {

        var commitID = $ (this).attr (\'id\');
        var name = $ ("#name_input_" + commitID).val ();
        var date_created = $ ("#created_input_" + commitID).val ();
        var status = $ ("#status_input_" + commitID).val ();
        var disbanded = $ ("#disbanded_input_" + commitID).val ();
        var dataString = {
            action: \'editCommittee\',
            id: commitID,
            name: name,
            date_created: date_created,
            status: status,
            disbanded: disbanded
        };

        console.log (dataString);
        // can place loading image here

        $.ajax({
            type : "post", 
            url: "/wp-admin/admin-ajax.php",
            data: dataString, 
            error: function(xhr, status, error) {
                var err = eval ("(" + xhr.responseText + ")");
                alert (err.Message);
            },
            success: function(response) {
                $ ("#name_" + commitID).html (name);
                $ ("#created_" + commitID).html (date_created);
                $ ("#status_" + commitID).html (status);
                $ ("#disbanded_" + commitID).html (disbanded);
                console.log ("Got this from server: " + response);
                console.log ("This is what happened to data: ");
                console.dir (dataString);
            }
        });
    });
我想你还是什么都没有,可能是你的URL。您可以尝试本地化admin-ajax.php 类似此答案的URL显示:https://stackoverflow.com/a/22007593/5623301