Answer the question
In order to leave comments, you need to log in
What am I doing wrong with the $.post request?
Hello!
I am not strong in JS, but I need to do ajax loading through the $.post function.
js code:
$.post(
"<?= Url::toRoute(['/user/default/get', 'id' => 1]); ?>", {
_csrf: "<?= Yii::$app->request->getCsrfToken() ?>",
},
function(data){
var info = $.parseJSON(data);
if(info.status == 'off') {
alert('off')
} else {
alert('on');
}
}
);
public function actionGet($id)
{
Yii::$app->response->format = Response::FORMAT_JSON;
$online = true;
if($online) {
$data['status'] = 'on';
return $data;
} else {
$data['status'] = 'off';
return $data;
}
}
Answer the question
In order to leave comments, you need to log in
If you do it like this:
$online = true;
if($online) {
$data['status'] = 'on';
return json_encode($data);
} else {
$data['status'] = 'off';
return $data;
}
Yii::$app->response->format = Response::FORMAT_JSON;
?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question