js代码
var id = $('#user_id').val(); //获取id为user_id元素的值
var num = $('#number').val();
$.ajax({
url : "test/index", //test是控制器名,index是方法名
dataType: "json", //传输的数据格式为json
type: "post", //请求方式为post
contentType: "application/json; charset=utf-8", //编码方式
data:JSON.stringify({
//JSON.stringify()的作用是JavaScript值转换为 JSON 字符串
id : id,
number : num,
}),
success: function (data) {
if(data.code == 0){
alert(data.msg);
}else{
alert(data.msg);
}
}
error: function () {
alert('请重试')
}
});
php代码
/*
记得写引用
use Yii;
use yii\web\Response;
*/
public function actionTest()
{
$id = Yii::$app->request->post('id'); //获取post传递的值
$num = Yii::$app->request->post('num');
//.....省略一些处理数据的逻辑
Yii::$app->response->format=Response::FORMAT_JSON; //返回json数据
return ['code'=>0,'msg'=>'success'];
}
阿邓啊
发布了10 篇原创文章 · 获赞 0 · 访问量 229
私信
关注
标签:Yii,json,ajax,num,post,接收数据,data,id
来源: https://blog.csdn.net/weixin_42814467/article/details/104058709