codeigniter AJAX和POST [英] Codeigniter AJAX and POST

查看:184
本文介绍了codeigniter AJAX和POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是解决将数据发送到POST在codeigniter一个AJAX脚本的最佳方法是什么?负载>视图(AJAX,$数据); 但是没有用户界面或用户现在我通过 $这个 - &GT加载与AJAX美景在该视图的动作。它只是通过脚本运行,并一点点返回POST数据的脚本加载后。我收到我的输入值到数据库,并输出一些其它值基于该数据在我的模型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?

我现在面临的问题是,我不能开辟另一种观点认为,因为AJAX视图是一个在焦点,但我需要这个AJAX的观点是暂时的,基本上做这件事,并发送至POST。

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.

推荐答案

有些人喜欢写Ajax的控制器,并张贴到他们完全,但你并不需要这样做。您可以处理在处理非Ajax请求的同一个控制器的要求。就个人而言,我只返回JSON,但你可以返回的HTML块是否适合你更好。

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.

您确切的问题是模糊的(实际code有助于澄清),但我认为你是在错误的轨道。不要使用处理过的任何一个观点。用你的控制器层,这是处理输入的请求。

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.

控制器方法的实施应对要么AJAX或非Ajax请求:

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)
}

$这个 - >输入 - > is_ajax_request()输入级 $ _ SERVER ['HTTP_X_REQUESTED_WITH'] 并检查它是否值 XMLHtt prequest 。这应该只是真实的,如果它是一个Ajax的要求。

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

我想我的问题是,我该如何处理的JavaScript没有看法?我该如何调用脚本和/或在哪里我把JS code控制器?我觉得这是错误的方向,解决了code的看法,但我没有看到怎么回事做到这一点。

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.

只要有可能,你应该把的javascript code在的.js 文件,并使用<脚本> 标记加载它,在一个HTML文档。唯一的另一个例外是把它在一个视图文件(文件的唯一目的在于构建最终的HTML输出)。换句话说,按照HTML的规则相同地方把JavaScript和遵循HTML的地方属于(视图)MVC的通常惯例。 Javascript的code不属于您的控制器。 JavaScript是没有处理你的数据,它是将数据发送到服务器。

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.

如果你想加载一个视图,然后的重定向(一定量的时候,我认为以后),你可以用的 JavaScript的 &LT;元&GT; 标记(但不使用meta标签,用js)。

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

这篇关于codeigniter AJAX和POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆