简体   繁体   English

为什么我的php函数没有被jquery $ .get执行

[英]why my php function is not executed by jquery $.get

I have a JavaScript in HTML file that call a php file in a $.get jquery API. 我在HTML文件中有一个JavaScript,可以在$ .get jquery API中调用php文件。 When I run the HTML file on my computer where WAMP is running (for php), I can't get the result of php function on the screen,neither the text of h1 tag nor the the output of print function. 当我在运行WAMP的计算机上运行HTML文件(对于php)时,无法在屏幕上获得php函数的结果,既没有h1标签的文本也没有打印功能的输出。 Thanks for a lot your answer. 非常感谢您的回答。

Regards 问候

Here is my html file AJAXTest.html 这是我的html文件AJAXTest.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="EN" dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/xml; charset=iso-8859-1" />

 <script type = "text/javascript"
      src = "jquery-1.3.2.min.js">

 </script>

<script type = "text/javascript">    
$(init);

function init(){
 alert("init passage 2" + " Andy");
  $.get("http://localhost/greetUser.php", { "userName": "Andy" }, processResult);
  alert("processresult passage" + data);
  $("#output").html(data);
}  

function processResult(data, textStatus){
alert("processresult passage" + data);
  $("#output").html(data);
}
</script>
<title>AJAXTest.html</title>
</head>
<body>
 <h1>Test AJAX</h1>

 <div id = "output">
   C’est la sortie par défaut
 </div>

</body>
</html>

and here is my php file 这是我的php文件

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="EN" dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
<head>
 <meta http-equiv="content-type" content="text/xml; charset=iso-8859-1" />
<title>greetUser.php</title>
</head>
<body>
<div>
  <h1>Réponse</h1>
<?php
 $userName = $_REQUEST["userName"];
 print "<p>Salut, $userName!</p> ";
?>
 </div>
 </body>
 </html>

It looks like everything should largely be working, but one thing that I can see that would break it, is that you have the 看起来一切都应该正常工作,但是我能看到的一件事会破坏它,那就是您拥有

  alert("processresult passage" + data);
  $("#output").html(data);

being called inside of the init() function, directly after the $.get call. 在$ .get调用之后直接在init()函数内部被调用。 At this point in the execution, the 'data' variable does not exist and using it will cause an error and halt all javascript from continuing, as the execution has to wait until the request is returned to be able to process it (in the processResult function, which you appear to be handling correctly) 在执行的这一点上,“数据”变量不存在,使用它会导致错误并停止所有javascript继续执行,因为执行必须等待直到返回请求才能处理它(在processResult中)功能,您似乎正在正确处理)

Removing those lines, I think should solve your problem. 删除这些行,我认为应该可以解决您的问题。

Try this: 尝试这个:

<script type = "text/javascript">    
init();

function init(){
    $.post("http://localhost/greetUser.php", { "username": "Andy" }, function(data,textStatus){
        $("#output").html(data);
    });     
}  
</script>

UPDATE UPDATE

I don't understand the downvote, but if you try this, it works: 我不了解该否决票,但如果您尝试这样做,它会起作用:

<script type = "text/javascript">    
init();

function init(){
    $.post("http://localhost/file.php", { "username": "Andy" }, function(data,textStatus){            
        $("#output").html(data);
    });     
}  
</script>
<title>AJAXTest.html</title>
</head>
<body>
 <h1>Test AJAX</h1>    
 <div id = "output">
   C’est la sortie par défaut
 </div>   
</body>
</html>

PHP file: PHP文件:

<?php
 $userName = $_POST["username"];
 print "<p>Salut, $userName!</p> ";
?>

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

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