简体   繁体   中英

why $.ajax does not work with me?

i don't know why $.ajax never called on my code ? i use xampp as localhost jquery called ok , so when i click on the button , the text below it changed .. but $.ajax part never execute ?

my page :

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test</title>
<link rel="stylesheet" href="layout/css/bootstrap.css">
<link rel="stylesheet" href="layout/css/font-awesome.min.css">
<link rel="stylesheet" href="layout/css/mystyle.css">
</head>
<body>
<div class="container">
<div class="alert alert-danger"> alerts alerts </div>
<div class="alert alert-danger"> alerts alerts </div>
<div class="alert alert-danger"> alerts alerts </div>
<button id="btn" class="btn btn-primary">cliquer ici </button>
</div>
<script type="text/javascript" src="layout/js/jquery-1.12.4.min.js">
</script>
<script type="text/javascript" src="layout/js/bootstrap.min.js"></script>
<script type="text/javascript" src="layout/js/myjs.js"></script>
</body>
</html>

my js :

$(document).on('click','#btn',function(){
    var ID =$(this).attr('id');
    $(this).html("loading ... ");
    $.ajax({
        type:"POST",
        url:"http://localhost/my_projects/testing/moremore.php",
        data:"id="+ID,
        success: function(html){
            $('.container').append(html);
            $('#btn').html("done");
        }
    });
    $(this).html("hmmmm ... ");
});

and my moremore.php :

<div class="alert alert-success"> alerts alerts by ajax </div>

1) Look for console.log errors or errors in network header
2) Make sure you .php script where you are requesting the ajax, has major headers to allow access control.
3) Sometimes, the headers doesn't allow to make you request.
4) When making request, keep your console opened (F12) and enable Log XMLhttpRequest

Try adding these headers to you moremore.php

header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Headers: X-Requested-With, Content-Type");
header("Access-Control-Allow-Origin: http://localhost");

Also to give it a try, check the ajax request with the ajax header, add this too in your moremore.php:

if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
     echo '<div class="alert alert-success"> alerts alerts by ajax </div>';
} else {
    echo 'no ajax';
}

This will give you an idea if ajax request is working fine or not.

New ajax js:

$(document).ready(function() {
  $('#btn').click(function() {
    var ID = $(this).attr('id');
    $(this).html("Loading ... ");
    $.ajax({
      type: "POST",
      url: "http://localhost/my_projects/testing/moremore.php",
      data: {
        id: ID
      },
      cache: false,
      xhrFields: {
        withCredentials: true
      },
      dataType: html,
      success: function(html) {
        $('.container').append(html);
        $('#btn').html("done");
      },
      error: function() {
        alert("Something went wrong");
        $("#btn").html("hmmmm, it was called... ");
      }
    });
  });
});

Do leave comments for more help!

I tested your code, and it pretty much works as is. The only thing that I changed was the ajax url from absolute http://localhost/my_projects/testing/moremore.php to relative moremore.php and I threw in the official jQuery CDN in case yours was borked.

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test</title>
<script src="http://code.jquery.com/jquery-3.0.0.min.js" integrity="sha256-JmvOoLtYsmqlsWxa7mDSLMwa6dZ9rrIdtrrVYRnDRH0=" crossorigin="anonymous"></script>
<link rel="stylesheet" href="layout/css/bootstrap.css">
<link rel="stylesheet" href="layout/css/font-awesome.min.css">
<link rel="stylesheet" href="layout/css/mystyle.css">
</head>
<body>
<div class="container">
<div class="alert alert-danger"> alerts alerts </div>
<div class="alert alert-danger"> alerts alerts </div>
<div class="alert alert-danger"> alerts alerts </div>
<button id="btn" class="btn btn-primary">cliquer ici </button>
</div>
<script>
$(document).on('click','#btn',function(){
    var ID =$(this).attr('id');
    $(this).html("loading ... ");
    $.ajax({
        type:"POST",
        url:"moremore.php",
        data:"id="+ID,
        success: function(html){
            $('.container').append(html);
            $('#btn').html("done");
        }
    });
    $(this).html("hmmmm ... ");
});
</script>
<script type="text/javascript" src="layout/js/bootstrap.min.js"></script>
<script type="text/javascript" src="layout/js/myjs.js"></script>
</body>
</html>

moremore.php :

<div class="alert alert-success"> alerts alerts by ajax </div>

Results in this expected output after a couple clicks:

它的屏幕截图

Try to wrap anonymous function, and change selector document to '#btn'

(function($) {
$('#btn').on('click',function(){
    var ID =$(this).attr('id');
    $(this).html("loading ... ");
$.ajax({
    type:"POST",
    url:"http://localhost/my_projects/testing/moremore.php",
    data:"id="+ID,
    success: function(html){
        $('.container').append(html);
        $('#btn').html("done");
    }
});
$(this).html("hmmmm ... ");
   });
})(jQuery);

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