[Fixed]-Error 403 forbidden on server request via ajax

1👍

Django requires a csrf token when making POST requests (unless you are using token based authentication but I am assuming you are not here). It’s just like when you need to include {{ csrf_token }} in form submit.

For more information about the why you need it and the purpose of csrf tokens: What is a CSRF token ? What is its importance and how does it work?

So for your problem, change your ajax call under add to this:

$.ajax({ 
        url: "http://138.49.184.143:3000/tasker/api/"+ownerId+"?key=f725ebbc9c",
        type: 'POST',
        data: { csrfmiddlewaretoken: '{{ csrf_token }}'}, // added csrf token.
        success: function(task) {
            var d = new Date(task.due);
            if(task){
                var newTask = {
                    onwerId: task.ownderId,
                    desc: task.desc,
                    due: d,
                    color: task.color,
                    complete: task.complete,
                    id: task.id
                };
                cb(newTask , null);
            }
            else{cb(null, 'error adding your task');}
        },
        error: function( xhr, status, errorThrown ) {
            alert( "Sorry, there was a problem! "  + errorThrown );
        },
    });

Leave a comment