如何在一个页面上运行多个Ajax调用 [英] How to run multiple ajax calls on one page

查看:317
本文介绍了如何在一个页面上运行多个Ajax调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在做一个Ajax化的用户界面与许多Ajax调用,用于创建,重命名,删除我的网页上各种各样的东西 - 什么是处理所有这些AJAX页面$ C $落后C最好的方法是什么?起初,我对每个呼叫(即Candy_Rename.php,Candy_Delete.php)不同的PHP文件。但我发现我的code基变得热烈,创建的文件的数量。

If I'm making an 'ajaxified' user interface with many ajax calls for creating, renaming, deleting various things on my page - what is the best way to handle all those "code behind" ajax pages? At first I had a different .php file for each call (i.e. Candy_Rename.php, Candy_Delete.php). But I found my code base becoming overwhelming with the number of files created.

现在我要送一个命令参数符合我的要求,然后我设法在一个文件中ajax_Candy.php与糖果所有的东西。因此,例如我的jQuery的要求是:

Now I'm sending a "command" parameter with my request, and then I manage all things related to Candy in one file "ajax_Candy.php". So for example my jQuery request will be:

$.get('ajax_Candy.asp', { command: 'insert', candyName: 'Jolly Ranchers' }, function (response) {
    alert(response.candyId);
}

这是一个很好的方式去了解它?

Is this a good way to go about it?

编辑:所有的答案是伟大的,说pretty的同样的事情。我不得不选择一个,我想提的安全性是其他任何人谁旅行沿着这条道路很重要 - 所以你去。我使用的是一个用户提到的switch语句。

All of the answers were great and said pretty much the same thing. I had to pick one and I thought mentioning security is important for anyone else who travels down this path - so there you go. I am using the switch statement as one user mentioned.

推荐答案

假设你正在处理在服务器端的安全问题,我没有看到你的方法有问题。该PHP文件必须确保请求者有权限来执行命令。你不会希望一个用户删除所有记录的......

Assuming that you are handling security issues on the server side, I don't see a problem with your method. The PHP file has to make sure that the requester has privilege to execute the command. You wouldn't want an user deleting all of your records...

这篇关于如何在一个页面上运行多个Ajax调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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