首页 > 解决方案 > jQuery 的 $.post 方法 AJAX 不起作用

问题描述

最近我一直在尝试 AJAX 和 jQuery。但不知何故 $.post 方法似乎不起作用。有人有解决方案吗?

这是我的代码。

<html>
<meta charset="utf-8">
 <head>
  <script type="text/javascript" src="jquery-3.3.1.js"></script>
  <script type="text/javascript">
    function send(){
      $.post('t.php', {stuff:1}, function(data){
        if(data == 'success'){
          alert('works');
       } 
      });
 }
 </script>
</head>
<body>
 <div id="btn" onclick="send()">CLICK</div>
</body>
</html>

我的t.php

<?php echo "success";?>

标签: phpjqueryajax

解决方案


它实际上可以工作,但您不知道如何php正确地从文件 中获取响应

更改 ajax 代码,如下所示:

$.post('t.php', {stuff:1}, function(data){
    if(data[0] == 's'){//changed here. data is an array not string
      alert('works');
   } 
  });

并在 php

<?php echo "s";?>

推荐阅读