Codeigniter AJAX y 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.

¿Cómo abordo esto?

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 temporary 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.

preguntado el 27 de agosto de 11 a las 18:08

1 Respuestas

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() es una función de la Clase de entrada eso lee $_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 código en un .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, luego redirect (after a certain amount of time I assume), you can do it with javascript o <meta> tag (but don't use a meta tag, use js).

Respondido 27 ago 11, 23:08

Thanks Wesley, I do have an is_ajax_request() call in my switch statement of a _remap function. And it is being called after the script loads. 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. - Chamilyan

do I put <script type="text/javascript" src="/js/file.js"></script> right in the controller? - Chamilyan

No, don't do that. I edited my answer, but without seeing any code I have no clue what you're actually trying to do. I suspect that if you add it, you will get decent help. I have no clue what your problem is now. - Wesley Murch

I'll be happy to show you my code but I think you'll have to see all of it to see what I'm trying to do. Can you shoot me an email? I'm trying to do something pretty non standard. - Chamilyan

Sorry I missed you earlier. I'm not sure why you can't just post it here? At least the concept (what you are trying to accomplish) I'm sure you would get help if you explained clearly your goal with code examples where applicable. In any case, you are free to contact me personally - that's why I put my email address in my profile - but I do recommend clarifying this post or creating a new one. If you edit this one I don't personally care, you can unaccept the answer, but if there were other answers I'd say make a new one. - Wesley Murch

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.