php - 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";?>
解决方案
它实际上可以工作,但您不知道如何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";?>
推荐阅读
- amazon-web-services - 如何通过 AutoScaling 动态增加 AWS EMR 上的 ulimit?
- clone - 乐克隆物体没有沉没
- django - 如何在 Django 中以编程方式将用户添加到组
- javascript - 我希望选择 HTML 表格单元格并在屏幕上可见
- c++ - libcurl 简单的 C 文件下载器示例立即返回
- powerbi - 刷新在 Power BI 服务上使用自定义函数的报表?
- python - 使用 Flask 部署机器学习 Web 应用的最佳实践
- xml - PowerShell:通过 xpath 解析 xml 节点并创建输出字符串
- json - 如何获取所需的json路径?
- jira - jira 中的打印优先级和分辨率