[Fixed]-Jquery ajax call not executing alert inside function call

1๐Ÿ‘

My guess is you submit your page.

Try this

<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function() {
  $("#some_id").on("click",function(event) {
    event.preventDefault(); // cancel the event - not needed on type=button though
    $.get("/some/url/", function(data) {
       window.console&&console.log('hey');
    });
  });
});
</script>
</head>
<body>
<button type="button" id="some_id">Click</button>
</body>
</html>

this assumes the return value from /some/url/ is valid object โ€“ the console should tell you

๐Ÿ‘คmplungjan

0๐Ÿ‘

The result is probably not in JSON format, so when jQuery tries to parse it as such, it fails. Try validating the JSON.

If the response is not in JSON format then try specifying the datatype. If it is returning a simple text then specify text.

$.get("/some/url/", function(data) {
    alert('hey');
}, "text");
๐Ÿ‘คJRodDynamite

0๐Ÿ‘

Try to use the Promise mechanism:

$.get("/some/url/").done(function() {
    alert( "1" );
  })
  .fail(function() {
    alert( "2" );
  })
  .always(function() {
    alert( "3" );
  });

Also, you can use $.ajax() function to test if connection is correct

๐Ÿ‘คgesiud

Leave a comment