使用jQuery AJAX请求回调 [英] AJAX request callback using jQuery

查看:128
本文介绍了使用jQuery AJAX请求回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的使用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.

下面是我的code:

**/*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,所以我的问题:?什么是能明确指定的数据我想回来的最好方法

有些想法我有:

  • 包装我所有的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文件,将只输出数乘以2 部分你当前的PHP。然后你根据你的当前的PHP的AJAX请求到新的单独的PHP。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆