我的一部分代码出现了这样的问题,得到以下逻辑。
当用户点击一个链接X时,一个post请求被发送到XAMPP Web服务器上的一个控制器,以打开链接X中的view2.此外,所有的附加在yii框架中,我是新手。
HTML view1:
<a id="article-title1" type="button" onclick="myFunction()">
Js:
function myFunction()
{
alert('allo my function');
dataString = "hallo";
$.ajax({
type:"POST",
url:"http://localhost/basic/web/example/hello",
data:{'dataString':dataString},
success:function (data) {
console.log("send success");
}
});
}
控制器。
public function actionHello() {
$nom = "david";
$prenom = "pascal";
return $this->render('information',[
'name' => $nom,
'prenom' => $prenom
]);
}
php view2:
<?php
/* @var $this yii\web\View */
use yii\helpers\Html;
$this->title = 'information';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class = "site-information">
<h1><?= Html::encode($this->title) ?></h1>
<p>
This is the information page
</p>
<p>
<b>name:</b><?= $name ?>
</p>
<p>
<b>firstname:</b><?= $firstname ?>
</p>
<code><?= __FILE__ ?></code>
</div>
此外,还有ajax url(http:/localhostbasicwebexamplehello。)在浏览器中工作正常。但是我不知道为什么在浏览器中收到ajax请求后,view2的渲染不显示。
当你发送ajax请求时,你总是需要返回一些东西给web浏览器。在这种情况下,你需要在服务器端返回一个已渲染的html,并将旧html替换为最新的js。