繁体   English   中英

Ajax发布到同一页面上的php无法正常工作

[英]Ajax post to php on same page not working

我试图将var从jQuery传递到PHP。

经过研究,我发现最常见的建议是使用带有Ajax的Post并使用PHP存储post var。

我的PHP技能非常好,但是我的JavaScript技能我不能说同样的话。

这是我正在做的,但似乎根本不起作用:

  // jquery libray included etc etc
  <script>
   $.ajax({
      type: "POST",
      url: "http://currentpage.com",
      data: "var=value",
      dataType: string
    });
 </script>
 </head>


<body>

<?php
if($_REQUEST["var"] == "value") {
    echo "var passed and stored";
}
?>
<script>
   $.ajax({
      type: "POST",
      url: "index.php",
      data: {var:'value'},
      dataType: 'text',
      success:function(data){
       // Test what is returned from the server
         alert(data);
      }
    });
 </script>

尝试

<script>
   $.ajax({
      type: "POST",
      url: "http://currentpage.com",
      data: {variablename:'value'},
      dataType: "text", //Available types xml, json, script, html, jsonp, text
      success:function(response){
       //Returned from server
       alert(response);
      }
    });
 </script>

暂无
暂无

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

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