简体   繁体   English

AJAX请求不起作用

[英]AJAX request doesn't work

I have some PHP script, HTML form and JS code(AJAX): 我有一些PHP脚本,HTML表单和JS代码(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. 没有JS代码一切正常。 PHP script works also fine and when I click button, after I receive needed information. PHP脚本工作也很好,当我点击按钮后,我收到所需的信息。 But when I try use AJAX and click button, I haven't any action. 但是当我尝试使用AJAX并单击按钮时,我没有任何动作。 I think, thant JS code is wrong and may be PHP also too. 我认为,JS代码是错误的,也可能是PHP。 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. 你正在设置dataType: json ,但你的PHP没有返回JSON。 Remove the dataType: 'json' line. 删除dataType: 'json'行。 Also don't forget the opening PHP tag: <?php . 另外不要忘记打开PHP标签: <?php
EDIT 2: OK, here's the code to get the meta tags and display them. 编辑2:好的,这是获取元标记并显示它们的代码。 PHP: 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: (上面的代码是你的问题 - 我假设它有效)Javascript AJAX成功函数:

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

HTML: 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 这对我有用...尝试这种方式直接把那个url像我做的和错误响应,这样如果它不起作用它会告诉你什么样的错误并删除dataType:'json'也是

<?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> 

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

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