简体   繁体   中英

Making an Ajax Call with JQuery?

I have been trying to change the content of a <div> with a JQuery Ajax Call. But nothing happens when I press the onclick <a> element. This is my current code that I'm testing with: Front.php:

<html>
<head>
<script type="text/javascript" src="jquery.js">
function testAjax()
{
   $.ajax({
     type: "GET",
     url: 'Back.php',
     data: "id=", // Not using this yet >.<
     success: function(data) {
          $('#test').html(data);
     }
   });
}
</script>
</head>
<body><div id="test">HELLO</div>
<a href="javascript:void(0);" onclick="testAjax()">Change this</a></body>
</html>

and here is my Back.php:

<?php
echo "test";
?>

Nothing happens when I click on "Change this" however the content in <div id="test"> should change to "test". Can someone help me with what I'm doing wrong? Thanks.

You didn't close your first script tag and open a new one for your inline script.

<script type="text/javascript" src="jquery.js"></script>
<script>
function testAjax()
{
    ...

You should apply contentType: "html" on it. May be that will help.

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