Jay Desai
Jay Desai

Reputation: 871

How to show response from server on client page after POST request using AJAX/jQquery

I have one client form which POST data to server's URL. When server receives POST request from client. It sends response back to client. I can see the response in inspect->network tab in google chrome. However, I want to show this response to client using alert or message.

Client page code:

<!DOCTYPE html>
<html>
<head>
    <title>Customer Form</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" type="text/css" href="style.css" />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            $("#submit").on('click', function(){
             var a=document.forms["myForm"]["EmailAddress"].value;
                if (a==null || a=="")
                {
                    $("#EmailAddress").css("border-color", "#963634");
                    alert("Please Fill Email Address");
                    return false;
                }
                else
                $.ajax({
                    url: 'http://127.0.0.1:8000/test/', 
                    type : "POST",
                    data : JSON.stringify($("#myForm").serializeArray()), 
                    success : function(result) {
                        alert('Data has been sent to API');
                        $("#myForm")[0].reset();
                    },
                    error: function() {
                        alert('failure');
                    }
                })
            });
        });
    </script>
</head>
<body>
    <div id="page-wrap">        
        <div id="contact-area">
            <center><img src="images/download.png" /></center><br/>
            <form id="myForm" action="" method="post" onsubmit="return validateForm()">
                <label>First Name:</label>
                <input type="text" name="FirstName"/>

                <label>Last Name:</label>
                <input type="text" name="LastName"/>

                <label for="DOB">DOB:</label>
                <input type="text" name="DOB"/>

                <label for="EmailAddress">Email Address:</label>
                <input type="text" name="EmailAddress" id="EmailAddress"/>

                <label for="Address1">Address 1:</label>
                <input type="text" name="Address1"/>

                <label for="Address2">Address 2:</label>
                <input type="text" name="Address2" />

                <label for="City">City:</label>
                <input type="text" name="City" />

                <label for="State">State:</label>
                <input type="text" name="State"/>

                <label for="PostalCode">Postal Code:</label>
                <input type="text" name="PostalCode"/>

                <input id="submit" type="button" name="submit" value="submit" class="submit-button">
            </form>
        </div>
    </div>          
</body>
</html>

Server code:

@api_view(['POST','GET',])
def TestView(request):
    if request.method == 'POST':
        print(request.body)
        data = json.loads(request.body.decode('utf-8'))
        customers_instance = Customers.objects.create(firstname=data[0]["value"],
                                            lastname=data[1]["value"],
                                            dob=data[2]["value"],
                                            emailaddress=data[3]["value"],
                                            address1=data[4]["value"],
                                            address2=data[5]["value"],
                                            city=data[6]["value"],
                                            state=data[7]["value"],
                                            postalcode=data[8]["value"])
        return HttpResponse('Data has been received by API')

I want to show server's response to client which is 'Data has been received by API'. I can see this response coming from the server in inspect as below: enter image description here

Upvotes: 1

Views: 1534

Answers (1)

dwpu
dwpu

Reputation: 268

Your code looks fine. Try to modify it in this way:

success: function(result) {
  window.alert(result);
  $("#myForm")[0].reset();
}

Upvotes: 1

Related Questions