Cookie的一个问题:已经解决了,合并response与request的结果集就可以了 [ 2.0 版本 ]
控制器代码:
namespace frontend\controllers;
use ....;
class RegisterController extends \core\frontend\FrontendController
{
......
public function actionIndex()
{
.......
if(Yii::$app->request->post())
{
//添加账号用户数据
$model->da_realname = Tkx::getCookie('nickname');
$model->save();
......
}else{
return $this->render('index',[
'model'=>$model,
]);
}
}
public function actionMessage(){
if(Yii::$app->request->get()){
......
if($res){
$data = array(
'status'=>'1'
);
}else{
$data = array(
'status'=>'0'
);
}
echo json_encode($data);
}else{
Tkx::wxstate('访问非法');
}
}
}
ajax请求
$.get("<?=\Yii::$app->urlManager->createUrl(['register/message']).'&phone_num=';?>"+phone_num,{},function(data){
if(data.status=='1'){
setCookie('code','code');
alert('验证码发送成功');
}else{
alert('验证码发送失败');
}
},'json');
核心类操作COOKIE的方法:
public static function getCookie($key){
$cookies = Yii::$app->response->cookies;
return $cookies->getValue($key,false);
}
问题:当getCookie方法使用response的时候,jquery发起的ajax函数的内容无效,反而是直接打印出了本应返回的JSON字符串;当getCookie方法改成request的时候,一切正常
共 0 个回答
没有找到数据。
jhomephper 帝都
注册时间:2015-05-05
最后登录:2021-04-20
在线时长:78小时1分
最后登录:2021-04-20
在线时长:78小时1分
- 粉丝32
- 金钱5330
- 威望10
- 积分6210