开发者

Ajax methods - which is better?

I have a bunch of insert, update, and delete operations I need to execute via Ajax. Which of the following would be a better approach?

  1. Individual methods for each function (e.g. delete_foo, insert_foo, update_foo, delete_bar, insert_bar, update_bar, etc.)

  2. A "master" method and just pass a parameter to distinguish between operations.

A benefit o开发者_高级运维f #2's approach would be that common things in the individual methods, such as validation or id decryption, etc. could be consolidated. However, it would also mean this master method would fairly large.

Having a master method call the individual method wouldn't be such a great idea, I think. The reason is that the individual methods, if stripped of the common tasks now handled by the master method, are one-liner codes (for the most part).


Something you should consider is that something destructive - such as a delete - should never be entirely controlled by a GET request. You should always use POST parameters for something like that. Beyond that i would say its entirely down to personal preference and what logically seems correct for your system. For example if you were using something like Zend Framework you could have separate actions for each operation but use a common set of validatons/decryptions within your controller within your constructor/init methods


An alternative to both of these methods is to use the REST rules. This makes use of the other HTTP Methods other than GET and POST.

For example

GET /foo            --select foo
POST /foo           --this would insert
PUT /foo            --this would update
DELETE /foo         --this would delete

GET /bar            --select bar
POST /bar           --this would insert
PUT /bar            --this would update
DELETE /bar         --this would delete

More details can be found here http://en.wikipedia.org/wiki/Representational_State_Transfer#RESTful_web_services.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜