【问题标题】:function save ajax error函数保存ajax错误
【发布时间】:2016-10-26 02:28:16
【问题描述】:

我已经做了功能,我可以在确认后添加一行。问题是,提交按钮后,表格不重新加载并显示错误功能警报。实际上数据成功保存,我必须刷新页面才能重新加载表格。这是我的ajax jquery代码:

function reloadPage()
   {
   window.location.reload()
   }

function save()
{
    $('#btnSave').text('saving...'); //change button text
    $('#btnSave').attr('disabled',true); //set button disable 
    var url;
 
    if(save_method == 'add') {
         url = "<?php echo site_url('activity/save')?>";
    } else {
        url = "<?php echo site_url('activity/update_activity')?>";
    }

 
    // ajax adding data to database
    $.ajax({
        url : url,
        type: "POST",
        data: $('#form-input').serialize(),
        dataType: "JSON",
        success: function(data)
        {
 
            
            $('#myModal').modal('hide');
           reloadPage();
 $('#btnSave').text('save'); //change button text
        $('#btnSave').attr('disabled',false); //set button enable 


    },
    error: function (jqXHR, textStatus, errorThrown)
    {
        alert('Error adding / update data');
        $('#btnSave').text('save'); //change button text
        $('#btnSave').attr('disabled',false); //set button enable 

    }
});
}
<button id="btnSave" onclick="save()" class="btn green">
"fa fa-save"> save</button>

我的控制器:

public function save() {
        $actype     	= $this->input->post('actype');
        $activity_name 	= $this->input->post('activity_name');
        $project  	= $this->input->post('project');
        $portion  	= $this->input->post('portion');
        $activity  	= $this->input->post('actid');
 

        $data = array(
        	'activity_type_id'=>$actype,
		'activity_name'	=>$activity_name,
		'project_id'	=>$project,
		'portion' 	=>$portion,
		'activity_id' 	=> $activity
            
        );
        $this->activity->insertactivity($data);
 
       redirect("activity/input");
    }

在我点击按钮保存后,警报(“添加/更新数据时出错”),但实际上在重新加载页面数据后已保存。 我的 ajax 代码中的代码错误在哪里?

【问题讨论】:

  • 你有两个函数保存???在控制器和脚本中?
  • 是的,是不是错了?
  • 我认为这是错误的。为什么你需要两个功能在一个按钮中具有相同的功能(保存)?
  • 保存在按钮重定向到 ajax javascript,并在 ajax javascript 调用控制器。这是 codeigniter。
  • 尝试重命名其中一个。

标签: javascript ajax codeigniter


【解决方案1】:

强制从服务器重新加载。

window.location.reload(true);

如果您未指定 true,则重新加载可能来自浏览器缓存(如果可用)。

此外,在控制器中,redirect("activity/input"); 不是对 AJAX 请求的适当响应。试试这样吧。

$this->activity->insertactivity($data);
echo json_encode(array('result' => TRUE));

您的控制器代码也可以更加简洁。考虑一下这个

public function save()
{
    $data = array(
            'activity_type_id' => $this->input->post('actype'),
            'activity_name' => $this->input->post('activity_name'),
            'project_id' => $this->input->post('project'),
            'portion' => $this->input->post('portion'),
            'activity_id' => $this->input->post('actid')
    );

    //Assuming insertactivity returns TRUE if the insert works and FALSE if not
    $results['result'] = $this->activity->insertactivity($data);
    echo json_encode($results);
}

可以在success函数中查看“结果”

success: function(data)
{
  if(data.result === true)
  {
    $('#myModal').modal('hide');
    reloadPage();
    $('#btnSave').text('save'); //change button text
    $('#btnSave').attr('disabled',false); //set button enable 
  } else {
    //do something to the DOM to tell about the problem
    //which probably means you should add that to your controller's response.
  }
},

【讨论】:

    猜你喜欢
    • 2011-10-11
    • 2011-01-12
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-10
    相关资源
    最近更新 更多