简体   繁体   English

jQuery Ajax调用似乎不起作用

[英]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. 我试图用jQuery Ajax调用一个页面,然后显示一个简单的响应(不管它是否起作用),但是似乎没有完成任何事情。 In FireBug I don't even get an error. 在FireBug中,我什至都不会出错。 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" . 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. 另外,如@blex在评论中所述,您应将绑定放在ondomready上下文中,以确保绑定之前已加载元素。

 $(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
      });
    });
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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