我创建了自定义的REST API,作为回应,我得到的是旧数据结果。我正在使用WP Engine,并且在wp-config.php文件中启用了缓存。我不想禁用缓存,只希望在不缓存数据的情况下重新设置我的api。我试图设置标题Cache-Control: no-cache, must-revalidate, max-age=0
,ob_flush()
,clearstatcache()
,但仍然存在相同的问题。另外,在我的ajax请求中,当我返回html数据时,它显示了旧条目。我在数据库中进行了交叉检查,它具有正确的值,但是ajax和api请求的响应是上一个。当我从仪表板上的WP Engine设置中清除缓存时,我在ajax和api响应中得到正确的结果。
这是我的代码api代码
register_rest_route( PLUGIN_DIRNAME.'/v1', '/data/status', array(
'methods' => 'GET',
'callback' => array( $this, 'get_status' )
));
public function get_status($request){
$response = array();
$progress = get_option('progress_percentage');
$name = get_option('form_submission');
$response["progress"] = $progress;
$response["form_id"] = $name;
if($progress == '100'){
$response["submission_status"] = __( 'successfull');
}
$result = new WP_REST_Response( $response, 200);
$result->set_headers( array(
'Cache-Control' => 'no-cache, must-revalidate, max-age=0'
) );
return $result;
}
在ajax响应中:
function ajax_response(){
$progress = get_option('progress_percentage');
if($progress == "100"){
$name = get_option('form_submission');
$submission_status = "<div class='complete'>Form submission successfull.</div>";
}
return array(
'message' => get_option('submission_stage'),
'html' => $submission_status,
);
}
有人可以帮我解决这个问题。从4小时开始,我一直在努力解决这个问题。如果有人建议使我的代码正常工作,那将是很大的帮助。
预先感谢。
最后,我设法解决了这个问题。如果有人遇到类似问题,我会在这里发布。我在api回调函数的开头和ajax请求回调函数中使用了wp_cache_flush()
。它为我解决了缓存问题。