时间:2021-07-01 10:21:17 帮助过:23人阅读
如:http://127.0.0.1:8080/MyBlog/Admin/Blog/insert?title=111&tags=111
写博客
if(M('article')->add($add)){
$data['message'] = '发布成功';
$data['icon'] = 6;
$this->ajaxReturn($data);
}else{
$data['message'] = '发布失败';
$data['icon'] = 5;
$this->ajaxReturn($data);
}
数据能提交也能返回。但是页面会跳转, 跳转后的url是get形式的。
如:http://127.0.0.1:8080/MyBlog/Admin/Blog/insert?title=111&tags=111
写博客
if(M('article')->add($add)){
$data['message'] = '发布成功';
$data['icon'] = 6;
$this->ajaxReturn($data);
}else{
$data['message'] = '发布失败';
$data['icon'] = 5;
$this->ajaxReturn($data);
}
因为你提交的按钮是button,button的type默认是submit,当你点击按钮时,先触发ajax,接着执行form表单提交
那你得手动阻止跳转:
$('#submit').on('click', function(e) {
e.stopPropagation();
e.preventDefault();
var action = $('#form1').attr('action');
var title = $('#title').val();
var tags = $('#tags').val();
var content = $('#textarea1').val();
$.post(
action,
{title: title, tags: tags, content: content},
function(data) {
layer.msg(data.message, {
icon: data.icon,
time: 2000 //2秒关闭(如果不配置,默认是3秒)
});
}
);
});