简体   繁体   中英

jQuery Ajax call doesn't seem to work

I am trying to call a page with jQuery Ajax and then display just a simple response if it worked or not, but it doesn't seem to get anything done. In FireBug I don't even get an error. What am I doing wrong here?

<script src="https://code.jquery.com/jquery-2.1.1.min.js" type="text/javascript"></script>

<script>
$('.doit').click(function (e) {
  e.preventDefault();
  $.ajax({
    url: "https://www.domain.com/page.php?do=it",
    type: "GET"
    success: function (data) {
        $(".result").html('<strong>Done!</strong>');
    },
    error: function (xhr, ajaxOptions, thrownError) {
        $(".result").html('<strong>Houston, we have a problem.</strong>');
    },
    timeout: 15000
  });
});
</script>

<a href="" class="doit">Do it</a>

<div class="result"></div>

You missed the comma after type: "GET" . Also, as mentioned by @blex in a comment, you should put your bindings inside an ondomready context, to make sure that your element has been loaded before binding.

 $(function(){
    $('.doit').click(function (e) {
      e.preventDefault();
      $.ajax({
        url: "https://www.domain.com/page.php?do=it",
        type: "GET",
        success: function (data) {
            $(".result").html('<strong>Done!</strong>');
        },
        error: function (xhr, ajaxOptions, thrownError) {
            $(".result").html('<strong>Houston, we have a problem.</strong>');
        },
        timeout: 15000
      });
    });
 });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM