Ajax呼叫无法在yii2中使用

My code:

$(".status").click(function () {
    var id= this.id;
    alert(id);

    $.ajax({
        type: "GET",
        url: '/dealer/change_status',
        data: "id="+id,
        success: function (response) {
    }});
});

On click, id alert show up, but ajax request doesn't work neither shows any error alert id and page refresh.

Check this,

$(".status").click(function () {
    var id= $(this).attr('id');
    alert(id);
});

Use ajax call like this. send ur id value as parameter. add one prameter to ur function change_status()

make sure you clicking on button or anchor tag. having class as status. and make sure you have proper url. I think you need to add base_url. other wish it will not work.

$(".status").click(function () {
    var id= this.id;
    alert(id);

    $.ajax({
        type: "GET",
        url: "<?php echo Yii::app()->baseUrl; ?>/dealer/change_status/"+id,
        success: function (response) {
    }});
});

I hope this will work for you.

You need to use

return false;

or

preventDefault();

to preventing page refreshing. After that, you need to check in your browser is ajax call exists ot not.

Better still, you can use it like this

$(".status").click(function () {
    var id= this.id;
    alert(id);

    var link = '". \yii\helpers\Url::toRoute("/dealer/change_status"). "';

    $.ajax({
        type: "GET",
        url: link,
        data: {id: id },
        success: function (response) {
        //Do this as stated below
        console.log(response); //This is to know the response from the server
    }});

   return false; //Prevent form submission twice

});

This code above should help you achieve your purpose. Feel free to write back