如果我不使用睡眠(3)。页面刷新并获取旧文件。或者我在js中使用计时器然后重新加载它的帮助。但我认为这是错误的方式。我不明白file_put_contents最终完成后如何返回$ ajax成功。
class OrderparamController extends FrontEndController
{
public function actionIndex($typeproduct = OrderService::TypePrintBannerCloth) {
$forms = $this->requireFile('order-forms.php');
$tplParams=array(
'id_order' => $typeproduct,
'forms' => $forms
);
$this->render('form',$tplParams);
}
/**
* Require file
*/
protected function requireFile($file_name){
$configFile = YiiBase::getPathOfAlias('application.cache').'/'.$file_name;
$handle = fopen($configFile, "r");
fclose($handle);
require $configFile;
return $forms;
}
/*
* Удалить параметр
*/
public function actionDeleteparamoption() {
$configFile='order-forms.php';
$forms=$this->requireFile($configFile);
$keyfield = $_POST['keyfield'];
$id_order = (int)$_POST['id_order'];
$numoption = $_POST['numoption'];
unset($forms[$id_order]['fields'][$keyfield]['options'][$numoption]);
if ($this->writeFile($forms,$configFile)) {
$answer=array(
'ok' => true,
);
} else {
$answer=array(
'ok' => false,
);
}
**sleep(3);**
echo json_encode($answer);
}
}
我使用js发送ajax
$('.js-btn-delete').on('click',function(){
var request ={
keyfield : $(this).data('keyfield'),
id_order : Var.id_order,
label : $(this).data('label'),
numoption : $(this).data('option'),
}
$.ajax({
url: '/orderparam/deleteparamoption',
type: 'POST',
data: request,
dataType: 'json',
async: true,
success: deleteOrderParamResult
})
});
function deleteOrderParamResult(data){
if (data.ok) {
var num=intVal($('.param-option').index($('.param-option:visible')));
document.cookie = "viewOption="+num;
location.reload();
} else {
alert('ошибка');
}
}