在 Yii 2.0 中发出 Ajax 请求?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29347905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Make an Ajax request in Yii 2.0?
提问by Matthew Smart
Right, i only want a very simple ajax request to get this working. im new with yii 2.0 framework you see.
是的,我只想要一个非常简单的 ajax 请求来让它工作。我是新的 yii 2.0 框架,你看到了。
in my view index.php:
在我看来 index.php:
function sendFirstCategory(){
var test = "this is an ajax test";
$.ajax({
url: '<?php echo \Yii::$app->getUrlManager()->createUrl('cases/ajax') ?>',
type: 'POST',
data: { test: test },
success: function(data) {
alert(data);
}
});
}
Now when i call this i assume that it should go to my CasesController to an action called actionAjax.
现在,当我调用它时,我假设它应该转到我的 CasesController 以执行名为 actionAjax 的操作。
public function actionAjax()
{
if(isset($_POST['test'])){
$test = "Ajax Worked!";
}else{
$test = "Ajax failed";
}
return $test;
}
EDIT::
编辑::
Ok great, so this works up to here. I get back the alert that pops up with the new value for $test. However i want to be able to access this value in php as ultimately i will be accessing data from a database and ill be wanting to query and do various other things.
好的,很好,所以这到这里为止。我取回了 $test 的新值弹出的警报。但是,我希望能够在 php 中访问此值,因为最终我将从数据库中访问数据,并且想要查询和执行其他各种操作。
So how do i now use this variable in php instead of just the pop up alert()?
那么我现在如何在 php 中使用这个变量,而不仅仅是弹出 alert()?
回答by Chinmay
Here is how you can access the post data in controller:
以下是如何访问控制器中的帖子数据:
public function actionAjax()
{
if(isset(Yii::$app->request->post('test'))){
$test = "Ajax Worked!";
// do your query stuff here
}else{
$test = "Ajax failed";
// do your query stuff here
}
// return Json
return \yii\helpers\Json::encode($test);
}
回答by Neeraj Kumar
//Controller file code
public function actionAjax()
{
$data = Yii::$app->request->post('test');
if (isset($data)) {
$test = "Ajax Worked!";
} else {
$test = "Ajax failed";
}
return \yii\helpers\Json::encode($test);
}
//_form.php code
<script>
$(document).ready(function () {
$("#mausers-user_email").focusout(function () {
sendFirstCategory();
});
});
function sendFirstCategory() {
var test = "this is an ajax test";
$.ajax({
type: "POST",
url: "<?php echo Yii::$app->getUrlManager()->createUrl('cases/ajax') ; ?>",
data: {test: test},
success: function (test) {
alert(test);
},
error: function (exception) {
alert(exception);
}
})
;
}
</script>
回答by Chinmay
You can include js this way in your view file:
您可以在视图文件中以这种方式包含 js:
$script = <<< JS
$('#el').on('click', function(e) {
sendFirstCategory();
});
JS;
$this->registerJs($script, $position);
// where $position can be View::POS_READY (the default), // or View::POS_HEAD, View::POS_BEGIN, View::POS_END
// 其中 $position 可以是 View::POS_READY(默认值), // 或 View::POS_HEAD、View::POS_BEGIN、View::POS_END
function sendFirstCategory(){
var test = "this is an ajax test";
$.ajax({
url: "<?php echo \Yii::$app->getUrlManager()->createUrl('cases/ajax') ?>",
data: {test: test},
success: function(data) {
alert(data)
}
});
}

