简体   繁体   English

jQuery ajax完成总是在Wordpress上失败

[英]jQuery ajax done always fails on Wordpress

I have the script below that erases a line on a MySQL database with ajax. 我下面的脚本使用ajax删除了MySQL数据库上的一行。 When the function is fired the line gets deleted from the database but I am always alerted "FAILED". 触发该功能后,该行将从数据库中删除,但始终会收到“失败”的提示。

function deleteRow(data){
if(confirm("Are you sure that you wish to remove this entry?\nThis cannot be undone")){
    $.ajax({
            type: "POST",
            url: "/wp-content/themes/Rexmed/deleterow.php",
            data: {id: data},
            dataType: "json"
        }).done(function() {
            alert("Success");
        }).fail(function() {
            alert("FAILED");
        });
}
}

This is deleterow.php 这是deleterow.php

<?php
require('../../../wp-blog-header.php');
if(!is_user_logged_in()){
auth_redirect();
die();
}

require ('../../../wp-config.php');
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$id = $_POST['id'];

if ($stmt = $mysqli->prepare("DELETE FROM customers WHERE id=?")) {
$stmt->bind_param("s", $id);
$stmt->execute();
$stmt->close();
}

Change the PHP and return JSON 更改PHP并返回JSON

<?php
    require('../../../wp-blog-header.php');
    if(!is_user_logged_in()){
        auth_redirect();
        die();
    }

    require ('../../../wp-config.php');
    $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
    $id = $_POST['id'];

    if ($stmt = $mysqli->prepare("DELETE FROM customers WHERE id=?")) {
        $stmt->bind_param("s", $id);
        if ($stmt->execute()) {
            $arr = array('success' => 'true');
        }else{
            $arr = array('success' => 'false');
        }
        $stmt->close();
    }

    echo json_encode($arr);

?>

and

.done(function(data) {
     if (data.success == 'true') {
        alert('you did it');
     }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM