使用 jQuery 的 AJAX 请求回调 [英] AJAX request callback using jQuery
问题描述
我是使用 jQuery 处理 AJAX 的新手,并编写了一个基本脚本来了解基础知识.目前我正在向同一个文件发布 AJAX 请求,我希望根据该 AJAX 调用的结果进行一些额外的处理.
I am new to the use of jQuery for handling AJAX, and have written a basic script to get the basics down. Currently I am POSTing an AJAX request to the same file, and I wish to do some additional processing based on the results of that AJAX call.
这是我的代码:
**/*convertNum.php*/**
$num = $_POST['json'];
if (isset($num))
echo $num['number'] * 2;
?>
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<style type="text/css">
td {border:none;}
</style>
</head>
<body>
<table width="800" border="1">
<tr>
<td align="center">Number To Send<br /><input type="text" id="numSend" size="40%" style="border:2px solid black;"></td>
<td align="center">Number Returned<br /><input type="text" id="numReturn" size="40%" readonly></td>
</tr>
<tr><td align="center" colspan="4"><input type="button" value="Get Number" id="getNum" /></td></tr>
</table>
<script>
$(document).ready(function () {
$('#getNum').click(function () {
var $numSent = $('#numSend').val();
var json = {"number":$numSent};
$.post("convertNum.php", {"json": json}).done(function (data)
{
alert(data);
}
);
});
});
</script>
</body>
</html>
这是我提交数字2"时得到的回复:
Here is the response I get if I submit the number '2':
4
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<style type="text/css">
td {border:none;}
</style>
</head>
<body>
<table width="800" border="1">
<tr>
<td align="center">Number To Send<br /><input type="text" id="numSend" size="40%" style="border:2px solid black;"></td>
<td align="center">Number Returned<br /><input type="text" id="numReturn" size="40%" readonly></td>
</tr>
<tr><td align="center" colspan="4"><input type="button" value="Get Number" id="getNum" /></td></tr>
</table>
<script>
$(document).ready(function () {
$('#getNum').click(function () {
var $numSent = $('#numSend').val();
var json = {"number":$numSent};
$.post("convertNum.php", {"json": json}).done(function (data)
{
alert(data);
}
);
});
});
</script>
</body>
</html>
显然我只对接收和使用数字4"感兴趣,因此我的问题是:准确指定我想要返回的数据的最佳方法是什么?
Obviously I'm only interested in receiving and using the number '4', hence my question: What is the best way to specify exactly what data I want returned?
我的一些想法:
- 将我所有的 HTML 包装在一个 if 语句中(即,如果 $num isset,则不输出 html;否则输出 HTML),但随后我正在回显 HTML,我宁愿不这样做.
- 设置一个单独的 PHP 脚本来接收我的 AJAX 调用:这就是我最初所做的,它工作得很好.但是,我有兴趣将所有内容都保存在一个文件中,并想探索这样做的可能方法.
我相信有一种优雅的方法可以做到这一点.感谢您的任何建议!
I'm sure there is an elegant way to do this. Thanks for any suggestions!
推荐答案
优雅的方式是拥有一个单独的 (!) PHP 文件,该文件将只输出当前的 number times 2
部分PHP.然后您生成一个从当前 PHP 到新的单独 PHP 的 AJAX 请求.
The elegant way would be to have a separate(!) PHP file that will only output the number times 2
part of your current PHP. Then you generate an AJAX request from your current PHP to the new separate PHP.
这篇关于使用 jQuery 的 AJAX 请求回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!