开发者

Codeigniter AJAX and POST

What is the best way to address an AJAX script that sends data to POST in codeigniter? Right now I am loading a view with the AJAX through $this->load->view('AJAX', $data); however there is no UI or user actions in the view. It's simply running through the script and returning POST data a little after the script loads. I receive the POST data in my model where I input the values into the DB and output some other values based on the data.

I need to open a real view, set metatags and re-direct the user to another website afterwards.

How do I address this?

The problem I'm facing is that I cannot open up another view because the AJAX view is the one that's in focus but I need this AJAX view to be t开发者_运维问答emporary that basically does it's thing and sends to POST.

Is there any convention that I can lookup/research to what I'm describing? Let me know what kind of clarification is needed if any.


Some people like to write "ajax" controllers and post to them exclusively, but you don't need to do that. You can handle the request in the same controller that handles the non-ajax request. Personally, I exclusively return json, but you can return chunks of HTML if that works better for you.

Your exact problem is vague (actual code would help clarify), but I think you are on the wrong track. Don't use a view for processing anything ever. Use your Controller layer, this is for handling input and requests.

Example of controller method responding to either ajax or non-ajax request:

function edit_user()
{
    $data['status'] = $this->user_model->save();
    if ($this->input->is_ajax_request())
    {
        // return json string with our update status
        // Something like: {"status":true}
        echo json_encode($data);
        exit;
    }
    // Load the non ajax view with the same data
    $this->load->view('users/edit', $data)
}

$this->input->is_ajax_request() is a function of the Input class that reads $_SERVER['HTTP_X_REQUESTED_WITH'] and checks if it's value is XMLHttpRequest. This should only be true if it's an "ajax" request.

You can make life easier by wrapping this in a class or function. No matter what you decide to do, don't use the view layer for processing data.


I think my problem is, how do I address javascript without a view? how do I call the script and/or where do I put the JS code in the controller? I felt it was the wrong direction to address the code in a view but I didn't see how else to do it.

Whenever possible, you should put javascript code in a .js file and use a <script> tag to load it, in an HTML document. The only other exception is putting it in a "view" file (a file that's only purpose is to construct your final HTML output). In other words, follow the same rules of HTML as to where to put javascript, and follow the usual conventions of MVC of where HTML belongs (in the view). Javascript code does not belong in your controller. Javascript is not processing your data, it is sending the data to the server.

I need to open a real view, set metatags and re-direct the user to another website afterwards.

If you want to load a view, then redirect (after a certain amount of time I assume), you can do it with javascript or a <meta> tag (but don't use a meta tag, use js).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜