AJAX调用后,CodeIgniter自动加载的模型变量丢失 [英] CodeIgniter autoloaded models' variables are lost after an AJAX call

查看:70
本文介绍了AJAX调用后,CodeIgniter自动加载的模型变量丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在创建一个应用程序,该应用程序需要按钮才能对使用自动加载的模型的控制器进行ajax调用.我给人的印象是,在Ajax调用之后,自动加载的模型中的变量将保留其值,但是由于某种原因,整个模型(包括其变量)都失去了新值.

I've been creating an application that requires buttons to make an ajax call to a controller that uses autoloaded models. I was under the impression that variables in an autoloaded model would retain their values after an Ajax call, but for some reason the entire model (including their variables) have lost their new values.

我是误解了自动加载功能的工作方式,还是使用Ajax与我有关系?

Am I misunderstanding the way the autoload function works, or does it have something to do with me using Ajax?

下面的参考代码.

自动加载:

$autoload['model'] = array('choice_model');

JQuery中的Ajax调用:

$( document ).ready(function() {
    var encounter = 1;

    $.ajax({
        type: "POST",
        url: baseURL+"Encounter/startEncounter",
        dataType: "json",
        data: "encounter_id=" + encounter,
        success: function(data) {
            $("#message-box").html(data);
            SetChoices();
        }
    });
});

function SetChoices() {

    $.ajax({
        type: "POST",
        url:  baseURL+"Choice/getChoices",
        dataType: "json",
        data: "",
        success: function(data){
            alert (data);
        }
    });
}

第一个ajax调用将Choice_model中的以下变量设置为"TestTrue":

public $test = 'TestFalse';

第二个ajax调用返回前一个变量,但其值现在再次为"TestFalse".

The second ajax call returns the previous variable, but it's value is now "TestFalse" once again.

谢谢您的时间,希望有人能帮助我.

Thank you for your time, I hope someone can help me out.

推荐答案

我是否误解了自动加载功能的工作方式?

Am I misunderstanding the way the autoload function works?

...是的.

自动加载表示在应用程序启动时自动加载(实例化)对象.因此,如果需要,我们将不需要加载它们两次.

Autoloading means the object is loaded (instantiated) automatically when the application starts. So we won't need to load them couple of times if needed.

注意:应该仅自动加载必要/必要的模型.

Note: Only necessary/essential models should be loaded automatically.

或者使用Ajax与我有关系

Or does it have something to do with me using Ajax

在这种情况下,XHR请求充当普通的HTTP请求.当您向Controller/method发送请求时,整个应用程序将在运行结果后运行并停止.

In this case, a XHR request acts as a normal HTTP request. When you send a request to a Controller/method the whole application runs and stops after serving the result.

因此,模型将丢失存储在其属性中的所有值.

So the model would lost all values stored in its properties.

我不确定,但是您可以将变量存储在Session中(通过使用CI中的$this->session->set_userdata()),然后再检索存储的值.

I'm not sure about this, but you could store the variables in Session (by using $this->session->set_userdata() in CI) and retrieve the stored values later.

这篇关于AJAX调用后,CodeIgniter自动加载的模型变量丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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