Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to get this ajax method to work but, for some reason my success function will not execute and I can not retrive informtion from my server. If any one could tell me what I am doing wrong I would apperciate it. My code is below.

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <script src="jquery3_2_1.js" type="text/javascript"></script>
    <script src="Test.js" type="text/javascript"></script>
</head>
<body>
<form class="ajax">
<input type="hidden" name="Item" value="Chicken">
<input type="hidden" name="Price" value="2.99">
<input type="hidden" name="Qty" value="5">
<button type="submit">ADD TO CART</button>
</form>
<div id="response1"></div>
</body>
</html>


my server code is here:
<?php
//UpdateCart.php

  echo "This is a test.";

?>


here is my jquery:

$(document).ready(function(){
    $('form.ajax').submit(function(){
      alert('Trigger');
  
        $.ajax({
            type: "post",
            url: "UpdateCart.php",
            dataType: "text",
            success: function(response){
              $('#response1').html('reponse variable equals: '+response);
            },
            error:function(){
              $('#response1').html('in error function');
          }
       });//end ajax function
    });//end submit function
});//end ready function


What I have tried:

When I run the code, the error function is always executed and I never reach the success function block of code. I tried changing the dataType to html but, that does not work. I replaced the submit function with the on function and used the submit parameter and I still can not execute any code in my success function. However, when I remove code that listens to the submit event, I am able to reach my success function. But, I do not want this behaviour from my program. I want to be able to control the request to the server with the submit event. I do not get back any errors from the error function nor do I see anything when I check the network tag in my browser's developer console. I even tried looking into apache's access and error logs but, I do not see any errors or any sign of my code communicating with the server. Any suggestions?

I tried this code and it also does not reach the success function:
my server code is here:

$(document).ready(function(){
    $('form.ajax').on('submit',function(){
      alert('Trigger');
  
        $.ajax({
            type: "post",
            url: "UpdateCart.php",
            dataType: "text",
            success: function(response){
              $('#response1').html('reponse variable equals: '+response);
            },
            error:function(){
              $('#response1').html('in error function');
          }
       });//end ajax function
    });//end submit function
});//end ready function


I can reach the success function with this code but it does not have the functionality that I want; it has the submit listener removed:
$(document).ready(function(){
      alert('Trigger');
  
        $.ajax({
            type: "post",
            url: "UpdateCart.php",
            dataType: "text",
            success: function(response){
              $('#response1').html('reponse variable equals: '+response);
            },
            error:function(){
              $('#response1').html('in error function');
          }
       });//end ajax function
});//end ready function
Posted
Updated 19-Dec-17 8:02am
v5
Comments
Afzaal Ahmad Zeeshan 18-Dec-17 7:49am    
Check the error message as well, I think there is a 404 and the page not found. Consider the following url: "/UpdateCart.php".
Sinisa Hajnal 18-Dec-17 8:04am    
error function has three parameters, status, error and the original request. Check them out.

1 solution

As mentioned in comments, you have to examine the parameters sent to the error function. But first you have to declare them. See jQuery.ajax() | jQuery API Documentation[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900