使用带有MVC的AJAX(CodeIgniter)时回显响应的正确方法

时间:2021-02-12 20:34:02

I have a form that submits to the submit_ajax method when submitted via AJAX. Now, when I receive it as an AJAX request, I want to return a JSON object.

我有一个表单,通过AJAX提交时提交给submit_ajax方法。现在,当我收到它作为AJAX请求时,我想返回一个JSON对象。

In this case, I have two options. What would be considered the right way to do it, following the MVC pattern?

在这种情况下,我有两个选择。按照MVC模式,做什么是正确的方法?

Option 1 Echo it from the controller

选项1从控制器回收它

class * extends CI_Controller 
{   
    public function submit_ajax()
    {
        $response['status'] = true;
        $response['message'] = 'foobar';
        echo json_encode($response);
    }
}

Option 2 Set up a view that receives data from the controller and echoes it.

选项2设置一个视图,从控制器接收数据并回显它。

class * extends CI_Controller
{
    public function submit_ajax()
    {
        $response['status'] = true;
        $response['message'] = 'foobar';
        $data['response'] = $response;
        $this->load->view('return_json',$data);
    }
}

//return_json view
echo json_encode($response);

3 个解决方案

#1


3  

The great thing about CodeIgniter is that in most cases it's up to yourself to decide which one you're more comfortable with.

关于CodeIgniter的好处在于,在大多数情况下,由您决定哪一个更适合您。

If you (and your colleges) prefer to echo through the Controller, go for it!

如果您(和您的大学)更喜欢通过控制器回应,那就去吧!

I personally echo ajax replies through the Controller cause it's easy and you have all of your simple scripts gathered, instead of having to open a view file to confirm an obivous json_encode().

我个人通过Controller回复ajax回复,因为它很简单,你收集了所有简单的脚本,而不必打开一个视图文件来确认一个重要的json_encode()。

The only time I'd see it to be logical to use view in this case is if you have 2 view files that echo's json and XML for instance. Then it can be nice to pass the same value to these views and get different outcome.

在这种情况下,我唯一一次看到使用视图是合乎逻辑的,如果你有2个视图文件,例如echo的json和XML。然后将相同的值传递给这些视图并获得不同的结果会很好。

#2


2  

The correct way according to the MVC pattern is to display data in the View. The Controller should not display data at any case.

根据MVC模式的正确方法是在视图中显示数据。在任何情况下,控制器都不应显示数据。

MVC is often seen in web applications where the view is the HTML or XHTML generated by the application. The controller receives GET or POST input and decides what to do with it...

MVC经常出现在Web应用程序中,其中视图是应用程序生成的HTML或XHTML。控制器接收GET或POST输入并决定如何处理它...

source: http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller

#3


1  

Usually when you have to show something on success in ajax funnction you need flags means some messages. And according to those messages you display or play in success function . Now there is no need to create an extra view. a simple echo json_encode() in controller is enough. Which is easy to manipulate.

通常当你必须在ajax funnction中显示成功时,你需要标记意味着一些消息。并根据您显示或播放成功功能的那些消息。现在无需创建额外的视图。控制器中的简单回声json_encode()就足够了。这很容易操纵。

#1


3  

The great thing about CodeIgniter is that in most cases it's up to yourself to decide which one you're more comfortable with.

关于CodeIgniter的好处在于,在大多数情况下,由您决定哪一个更适合您。

If you (and your colleges) prefer to echo through the Controller, go for it!

如果您(和您的大学)更喜欢通过控制器回应,那就去吧!

I personally echo ajax replies through the Controller cause it's easy and you have all of your simple scripts gathered, instead of having to open a view file to confirm an obivous json_encode().

我个人通过Controller回复ajax回复,因为它很简单,你收集了所有简单的脚本,而不必打开一个视图文件来确认一个重要的json_encode()。

The only time I'd see it to be logical to use view in this case is if you have 2 view files that echo's json and XML for instance. Then it can be nice to pass the same value to these views and get different outcome.

在这种情况下,我唯一一次看到使用视图是合乎逻辑的,如果你有2个视图文件,例如echo的json和XML。然后将相同的值传递给这些视图并获得不同的结果会很好。

#2


2  

The correct way according to the MVC pattern is to display data in the View. The Controller should not display data at any case.

根据MVC模式的正确方法是在视图中显示数据。在任何情况下,控制器都不应显示数据。

MVC is often seen in web applications where the view is the HTML or XHTML generated by the application. The controller receives GET or POST input and decides what to do with it...

MVC经常出现在Web应用程序中,其中视图是应用程序生成的HTML或XHTML。控制器接收GET或POST输入并决定如何处理它...

source: http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller

#3


1  

Usually when you have to show something on success in ajax funnction you need flags means some messages. And according to those messages you display or play in success function . Now there is no need to create an extra view. a simple echo json_encode() in controller is enough. Which is easy to manipulate.

通常当你必须在ajax funnction中显示成功时,你需要标记意味着一些消息。并根据您显示或播放成功功能的那些消息。现在无需创建额外的视图。控制器中的简单回声json_encode()就足够了。这很容易操纵。