简体   繁体   中英

AJAX request doesn't work

I have some PHP script, HTML form and JS code(AJAX):

if(isset($_POST['site'])){

$homepage = file_get_contents("http://".$_POST['site']);
preg_match('%<meta.*name="keywords".*content="(.*)"\s+/>%U', $homepage, $regs);

if(count($regs))
{
$myString = implode('', $regs );  
 print_r($myString);
}
}


?>


<form id=payment method="post" name="forma1">
<label for=name>ENTER www.bbc.com:</label>
<input id="name" type=text placeholder="Write here..."         name="site">
<input type="submit" value="START" name="searchbutton" id="sb">
</form>

<script src="jquery.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function(){
         $('#payment').submit(function(e){
            e.preventDefault();
           $.ajax({
                 type: "POST",
                url: $(this).attr('action'),
                data: $(this).serialize(),
                dataType: 'json',
                success: function(data)
                {
                    alert("OK, AJAX IS WORKING");
                }
            });
        });
    });
    </script>

Without JS code everything works fine. PHP script works also fine and when I click button, after I receive needed information. But when I try use AJAX and click button, I haven't any action. I think, thant JS code is wrong and may be PHP also too. Please, experts, who can help me and modify the code?

EDIT: Here's the problem. You're setting dataType: json , but your PHP isn't returning JSON. Remove the dataType: 'json' line. Also don't forget the opening PHP tag: <?php .
EDIT 2: OK, here's the code to get the meta tags and display them. PHP:

$homepage = file_get_contents("http://".$_POST['site']);
preg_match('%<meta.*name="keywords".*content="(.*)"\s+/>%U', $homepage, $regs);
if(count($regs)) {
  $myString = implode('', $regs );  
 echo $myString;
}

(the code above is yours from your question - I'm assuming it works) Javascript AJAX success function:

success: function (data) {
  $('#metaTags').text(data)
}

HTML:

<div id="metaTags"></div>

this is working for me...try this way directly put there url like i did and error response so that if it not works it will show you what kind of error and remove dataType:'json' too

<?php  if(isset($_POST['site'])){

 var_dump($_POST); 
 exit;


}
?>
<html>

<head>
 <script src="http://code.jquery.com/jquery-latest.js"></script>    
<script type="text/javascript">
    $(document).ready(function(){
         $('#payment').submit(function(e){
            e.preventDefault();
           $.ajax({
                 type: "POST",
                url: 'test.php',
                data: $(this).serialize(),
                success: function(data)
                {
                    alert("OK, AJAX IS WORKING"+data);
                },
                 error:   function(xhr, ajaxOptions, thrownError) { alert(xhr.status);
                                         } 
            });
        });
    });
    </script>
</script>

</head>     

<div>
<form id="payment"    method="post" name="forma1">
        <label for=name>ENTER www.bbc.com:</label>
        <input id="name" type=text placeholder="Write here..."         name="site">
        <input type="submit" value="START" name="searchbutton" id="sb">
</form>

</div>  


</html> 

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