【问题标题】:Record is being added to the DB with delay记录被延迟添加到数据库中
【发布时间】:2014-09-17 12:12:51
【问题描述】:

我使用的是 Cakephp(在开发模式下),我的数据源是 database/Mysql。

在我的系统中,我有以下过程:

  1. 访客选择 6 项
  2. 访问者点击按钮
  3. 单击按钮时,我使用 jquery $.post 将数据发送到 php 函数,该函数获取该数据并将其添加到数据库中。
  4. 在那之后(或者可能不像看起来那样),他们的访问者被重定向到“支付页面”。

这是负责将数据发送到 php 函数“句柄”的 jquery 脚本。

$('.next-step').on('click', function(){

    var paymentPage = $(this).attr('data-href');

    var slotsData = [];
    var i = 0;
    $('.slot').each(function(){
        slotsData[i] = {
            'img-id': $(this).attr('data-id'),
            'img-src': $(this).attr('data-src'),
            'img-high-src': $(this).attr('data-high-src'),
            'img-page': $(this).attr('data-page')
            };
        i++;    
    }); 

    var setid = 0;

    $.post('/sets/handle', {k:'',setid:setid,setData:slotsData}, function(data){
                window.location = paymentPage;
    });

    return true;
});

这是php函数句柄

public function handle()
{
    $this->autoRender = false;
    $this->request->onlyAllow('ajax');

    $set_id = (int)$this->request->data('setid');
    $images = serialize($this->request->data('setData'));


    //Key
    $cookie_key = $this->Cookie->read('visitor_key');
    if(isset($cookie_key) && $cookie_key != null)
    {
        $key = $cookie_key;
    }
    else
    {
        $key = $this->newKey();
        $this->Cookie->write('visitor_key', $key);
    }


    //Edit Mode
    if(isset($set_id) && $set_id > 0)
    {
        /*Some other irrelevant code */         
    }
    else //Creating a new set
    {
        $this->Set->create();
        $this->Set->save(array(
            'images' => $images,
            'visitor_key' => $key,
            'quantity' => 1
        ));

        AppController::logActivity( array('visitor_key' => $key, 'instagram_id' => 0, 'instagram_username' => ''), 'Created New Set', $this->Set->id );

        return json_encode(array('status' => 'created','setid' => $this->Set->id,'visitor_key'=>$key));
    }


}

这是php函数支付

public function payment() {

    $this->set('title_for_layout', 'Review Your Order');
    /*
    debug( date("d-m-y H:i:s") );
    */                  
    $sets = $this->Set->findAllByVisitorKeyAndOrderId($this->Cookie->read('visitor_key'),'0');
    /*
    debug($sets);
    */      
    $onesetcost = Configure::read('Ecom.setcost');
    $visitor_key = $this->Cookie->read('visitor_key');

    $total_sets = 0;
    foreach($sets as $set){
        $total_sets += $set['Set']['quantity'];
    }
    $shippmentCost = $this->shippmentCostByQuantity($total_sets);           

    $this->set(compact('sets','onesetcost','visitor_key','shippmentCost'));

}

所以问题是进入支付页面后,“sets”变量不包含最近添加的新数据。

为了找到问题,我将记录的创建时间(我在 sets 表中有一个“created”字段)与加载支付页面的时间进行了比较。 (见注释命令)。

似乎是在显示付款页面后 30 秒左右添加记录。 这怎么可能?

我的意思是,将记录添加到数据库后正在处理重定向。 我以为是缓存,但我仍在使用默认的文件引擎缓存,它似乎不缓存记录,而只是缓存数据库方案。

我一直在寻找任何相关的问题,但一无所获, 我调试并尝试了几乎所有想法,但仍然没有找到问题的根源。

谢谢。

【问题讨论】:

    标签: php jquery sql cakephp


    【解决方案1】:

    将您的 window.location = paymentPage; 代码包装在成功处理程序中。

    例如:

    $.post('/sets/handle', {k:'',setid:setid,setData:slotsData}, function(data){
        // Do nothing in here
    }).done(function( msg ) {
        window.location = paymentPage;
    });
    

    发生的情况是用户在请求发生的同时被重定向到新页面。如果你等到请求完成,那么你应该没问题。

    更多关于 jquery ajax 请求的信息在这里: http://api.jquery.com/jquery.ajax/

    【讨论】:

    • 我确信.done.success 基本相同。我会尝试并更新你。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多