[Fixed]-Integrating ajax with django

1👍

You function index doesnt returns any http response. Just after print return a JsonResponse. Also all post calls must end with a trailing slash. If you face any such issue related to client end, check the console and you will find the cause.

Edited:

The issue is that you didnt included jquery in your html. I’ve included jquery and instead of absolute url, I’ve used relative url. The only issue remains is there is no element with id data. Fix that and you’ll be good to go. 🙂

<head>
<head>
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script>
function change()
{   
    var y = document.getElementById("one");
    y.value = 'X';
    $.ajax({
        url:"/handler/",
        type:"POST",
        data:{},
        cache:false,
        success:function(data) {
            var x = document.getElementById(data);
            x.value = 'O';
            alert("sucess");
        },
        error:function(error) {
            alert(error);
        }
    });
}
</script>
</head>
<body>
<input type = "button" id = "one" onclick="change()"></input>
</body>
</html>

0👍

You need to append a / to your url in the ajax call:

$.ajax({
    url:"127.0.0.1:8000/handler/",
    type:"POST",
    data:{},
    . . . 

Leave a comment