使用AJAX和方法POST提交表单

使用AJAX和方法POST提交表单

问题描述:

I have a difficulty in implementing an MVC system.
I have a simple comment system, which makes the record when sent via POST or via AJAX.

a) Should I separate the 2 controllers in Comment_Ajax and Comment_Post
b) or should I just keep a controller and and check the sent method (is_ajax()) to perform the necessary actions.

Form method 'Post' redirects to the page of the post and form method 'Ajax' only makes a reload in the comments.

我在实现MVC系统时遇到了困难。
我有一个简单的评论系统,这使得 通过POST或通过AJAX发送时的记录。
p>

a) strong>我应该在Comment_Ajax和Comment_Post中分离2个控制器
b) strong>或者我应该只保留一个控制器并检查发送的方法(is_ajax())以执行必要的操作。
p>

表单方法'发布'重定向 到帖子的页面和表单方法'Ajax'只会在评论中重新加载。

p> div>

I would go with version b, keeping related methods together. Keep DRY: Don´t Repeat Yourself.

What you are doing are 2 actions (methods/functions) of one controller, which is pefectly valid MVC. Usually a controller has one action "render" (display page if no parameters are present), and can have multiple actions like save, update, submit, render_ajax etc.

It is not a good option to create more controllers.

I didn't understand your statement "Form method 'Post' redirects to the page of the post and form method 'Ajax' only makes a reload in the comments".

Generally we can comment to a post if we are in corresponding post page, again you are saying 'post' will redirect to post page.

Based on Client requirement we will go for 'reload the page' or 'just updating commnets section with AJAX'.

And moreover, what ever is the process you follow, Just write one method like saveComment(); and call this method, through form action if it is Post, or mention this method in AJAX call.