jQuery 在 codeigniter(flashdata) 中返回一个 json 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4746164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Returning a json object in codeigniter(flashdata)
提问by Philip
How does one go about returning a json object in this case(messages) to a view(admincp_index). The method below works fine but I would really like to spice it up with some animations
在这种情况下如何将 json 对象(消息)返回到视图(admincp_index)。下面的方法工作正常,但我真的很想用一些动画来调味
Regards, Phil
问候, 菲尔
/* AdmincontrolPanel */
function index()
{
$data['messages'] = $this->session->flashdata('messages');
$data['content'] = 'admincp/admincp_index';
$this->load->view('backend/template', $data);
}
function applicant()
{
$id = $this->input->post('id');
if($this->input->post('accept'))
{
if($this->admincpModel->accept_applicant($id) == TRUE)
{
$this->session->set_flashdata('messages', '<div class="ok">Applicant Added!</div>');
redirect('admincp');
}
}
/* admincp_index */
if($messages){
// echo messages
}
回答by Nea
use code igniter 's output class to response json.
使用代码点火器的输出类来响应 json。
$this->output
->set_content_type('application/json')
->set_output(json_encode(array('foo' => 'bar')));
回答by JHoye
There are three things that need to be kept in mind:
需要牢记以下三点:
The browser may cache the JSON response, so it's a good idea to append a time-stamp to the end of the URL to keep the data coming in fresh. (This is true of the GET method, not necessarily so with POST though).
The content type of the JSON response needs to be "application/json" or "text/javascript".
The
json_encode
function was included with PHP 5.2, so older environments may not be able to use it, and you'll have to either get the module installed or write your own encoding class.
浏览器可能会缓存 JSON 响应,因此最好在 URL 末尾附加时间戳以保持数据新鲜。(GET 方法确实如此,但 POST 不一定如此)。
JSON 响应的内容类型需要是“ application/json”或“ text/javascript”。
该
json_encode
函数包含在 PHP 5.2 中,因此较旧的环境可能无法使用它,您必须安装该模块或编写自己的编码类。
I'm doing some work on a server running PHP 5.1.6, and I don't need to serialize any complex types, so I've found the technique shown below to work fine. I'm using a simple "JSON view" which sets the correct content type in the response header, and emits a JSON string which was manually concatenated in the controller.
我正在运行 PHP 5.1.6 的服务器上做一些工作,我不需要序列化任何复杂类型,所以我发现下面显示的技术可以正常工作。我正在使用一个简单的“JSON 视图”,它在响应标头中设置正确的内容类型,并发出一个在控制器中手动连接的 JSON 字符串。
Phil, jQuery effects/animations could make use of the returned JSON data in the successcallback function. In the example below I'm just showing messagein an alert box.
Phil,jQuery 效果/动画可以在成功回调函数中使用返回的 JSON 数据。在下面的示例中,我只是在警报框中显示消息。
Client-side Code:
客户端代码:
// the jQuery POST URL includes a time stamp
var now = new Date();
$.ajax({
type: "POST",
url: "page/post/" + now.valueOf().toString(),
data: {},
dataType: "json",
success: function (result) {
alert(result.message);
}
});
Controller (/application/controllers/page.php):
控制器(/application/controllers/page.php):
class Page extends CI_Controller {
function __construct()
{
parent::__construct();
}
function index()
{
}
function post($TimeStamp)
{
/* process request... $Timestamp may or may not be used here. */
$data['json'] = '{"message":"The post was handled successfully."}';
$this->load->view('json_view', $data);
}
}
View (/application/views/json_view.php):
查看(/application/views/json_view.php):
<?php
$this->output->set_header('Content-Type: application/json; charset=utf-8');
echo $json;
?>
回答by navruzm
You doing wrong way. If you want to get json object, AJAX is the best way to handle this. In your admincp_index view (with jquery)
你做错了。如果您想获取 json 对象,AJAX 是处理此问题的最佳方式。在您的 admincp_index 视图中(使用 jquery)
$.ajax({
type: 'POST',
url: 'controller/applicant',
data: 'your post data',
success: function(response) {
var response = $.evalJSON(r);
if(response.message) {
//do some animation
}
}
});
applicant method
申请人方法
function applicant()
{
$id = $this->input->post('id');
if($this->input->post('accept'))
{
if($this->admincpModel->accept_applicant($id) == TRUE)
{
echo json_encode(array('message'=>'<div class="ok">Applicant Added!</div>'));
exit();
}
}