HTML代码
<form onsubmit="comment()" id="form_comment" action="javascript:void(0)">
<input type="text" name="article_id" />
<li class="list-group-item">
<textarea name="content" class="form-control" rows="10" style="resize:none;" required></textarea>
<button class="btn btn-default" type="submit">提交</button>
</li>
</form>
JS代码
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
<script>
function comment() {
$.ajax({
url:"action/commentAction.php", //提交地址
type:"post", //提交方式
data:$('#form_comment').serialize(), //表单内所有信息
success: function (result) {
var obj=JSON.parse(result)
if(obj.type=="请登录")
{
window.location.href='login.php';
}
else if(obj.type=="发表成功")
{
location.reload()
}
else
{
alert("异常")
}
},
error : function(result) {
console.log(result)
}
});
};
</script>