Md Samiul Alim Md Samiul Alim - 5 months ago 17
HTML Question

How can i use Ajax success data in same template or somewhere else?

$.ajax({
type: "POST",
url: "{% url 'my_url' %}",
async: false,
data: formData,
cache: false,
contentType: false,
processData: false,
success: function(data) {
console.log(data)
name = data.name
error: function(e) {
alert(e.message);
}
});


Here i want to use this name variable in template(In Django Template). How can i use directly?
Means can i use like

<html>
{% if myname = = name %}
<input type="text" value="name" disable</input>
{%endif%}
</html>


Here name is a variable coming from django views in ajax success.

Answer

User your ajax response like this

$.ajax({
            type: "POST",
            url: "{% url 'my_url' %}",
            async: false,
            data: formData,
            cache: false,
            contentType: false,
            processData: false,
            success: function(data) {
                console.log(data)
                name = data.name
            $("#anyID").val(name); // like you can populate the values using id or class
            error: function(e) {
                alert(e.message);
            }
        });