gaurav-r
12/10/2017 - 11:23 PM

AJAX request with stringify and CSRF

$.ajax({
    type: "POST",
    url: "/products/importlist/posttoshop/",
    data: JSON.stringify(product_post_data),
    beforeSend: function (xhr) {
        xhr.setRequestHeader("X-CSRFToken", getCookie("csrftoken"));
    },
    contentType: "json",
    success: function (data) {
        console.log("SUCCESS");
        console.log(data);
        switch (data["result"]){
            case "success":
              // success action
                break;
            case "error":
                // error action
        }
    },
    error: function () {
        // Request failed and error
    }
})