Answer the question
In order to leave comments, you need to log in
How to properly implement ajax in yii2?
Need to be implemented jQuery.ajax
in the project yii2 advanced.
The problem occurred when setting the jQuery property of the ajax method in the url.
<script>
$(document).ready(function () {
$(".category-div").click(function () {
$.ajax({
url: "../../test.php",
dataType: "html",
data: "id="+this.id,
success: function(data){
$("#conteiner").html(data);
},
error: function () {
$("#conteiner").html("ERROR");
}
});
});
});
</script>
url
a file is installed that is not in web
the frontend
. url: /common/widget/views/test.php
or even /frontend/views/blog/test.php
(provided that such a file is there, of course), it throws an error 404
. test.php
:<?php
?>
<!DOCTYPE html>
<html>
<head>
<title>exa</title>
</head>
<body>
<?php
return " <b>Получены параметры с сервера: </b> id = ".$_GET['id'];
?>
</body>
</html>
<?php
?>
<!DOCTYPE html>
<html>
<head>
<title>exa</title>
</head>
<body>
<?php
return " <b>Получены параметры с сервера: </b> id = ". Yii::$app->request->get('id');
?>
</body>
</html>
GET portfolio/test.php?id=4 500 (Internal Server Error) jquery. js:9203
ajax
in Yii2
???
Answer the question
In order to leave comments, you need to log in
1. Where did php come from at the end, if it is yii2 with its beautiful urlManager?
2.
/frontend/views/blog/test.phpit's probably a path, not a url.
$this->registerJs('
$(".category-div").click(function () {
$.ajax({
url: "'.\yii\helpers\Url::toRoute(['blog/test','id'=>$someid]).'",
dataType: "html",
data: "id="+this.id,
success: function(data){
$("#conteiner").html(data);
},
error: function () {
$("#conteiner").html("ERROR");
}
});
});
');
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question