douya5194
2017-12-13 19:08
浏览 47
已采纳

Ajax加载页面+ JQuery点击不起作用[重复]

This question already has an answer here:

Why does my ajax not work?

These are my pages.

I'm using JQuery to check click event and using ajax to load the responder page.

What is wrong in this code?

index.php

<html>
  <head>
    <title>Test</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script>
      $(document).ready(function() {
          $('#clicket').click(function() {
            $.ajax({
              url: 'res.php?rand=' + Math.random(),
              type: 'GET'
              success: function(results) {
                alert(results);
              }
            });
          });
        });
      </script>
    </head>
  <body>
    <button id="clicket">Hi</button>
  </body>
</html>

Responder page:

res.php:

<?php
  echo "Worked!";
?>

UPDATE: I changed my javascript code above.

</div>
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanci6484 2017-12-13 19:11
    已采纳

    Put your logic in a document ready. Since your script is in your head, the markup in the body hasn't been loaded into the DOM yet. In order to make your script wait until the DOM has been built, put the logic in a document ready to delay it until that time.

    $(document).ready(function() {
      $('#clicket').click(function() {
        $.ajax({
          url: 'res.php?rand=' + Math.random(),
          type: 'GET',
          success: function(results) {
            alert(results);
          }
        });
      });
    });

    </div>
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dtvx3420 2017-12-13 19:12

    Try using full URL for your ajax call. If you are on live server then write your full domain name, like www.your_domain.com/res.php?rand=.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题