简体   繁体   中英

Ajax request not responding

I'm trying to do a ajax request by clicking a span element.

Here the HTML of the span:

<span class="fake-link do-delete" data-id="'. $delid .'">Delete</span>

Here is my jQuery ajax request:

$(".do-delete").click(function() {

        var item = $(this).attr("data-id");

        $.ajax({

            type: "POST",
            url: "../assets/includes/ajax/delete-item.php",
            data: item,
            cache: true,

            success: function(data) {

                $(".test").append(data);

                // alert("De categorie met zijn respectievelijke onderdelen zijn verwijdert.");

            }

        });

    });

Here is my current PHP, to catch the data send by ajax:

if (isset($_POST['item'])) {

        $id = $_POST['item'];

        echo 'test';

}

But the test isn't coming through, so the item isn't posted...

What am I doing wrong?

data should be data: {'item' : item}

$(".do-delete").click(function() {

        var item = $(this).attr("data-id");

        $.ajax({

            type: "POST",
            url: "../assets/includes/ajax/delete-item.php",
            data: {'item' : item},
            cache: true,

            success: function(data) {

                $(".test").append(data);

                // alert("De categorie met zijn respectievelijke onderdelen zijn verwijdert.");

            }

        });

    });

The problem here is that you are passing wrong parameters to ajax call, you want to pass data as JSON object, so instead of data: item, , write data: {'item': item},

NOTE: also be sure to be opened developer tools->network tab, in most cases it will let you know where the problem is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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