$(function () {
$("#comment_submit").click(function (event) {
event.preventDefault();
user_comment =$("#comment_input").val();
console.log(user_comment);
if (!user_comment) {
alert('请输入评论内容');
return
}
$.ajax({
url:"/cms/comment/",
type:"post",
dataType:"json",
data: {
'user_comment':user_comment
},
success:function (data) {
alert("评论成功");
$("#comment_input").val("");
}
});
})
});
在原生写法中,如果基本的格式没有出现,有可能是缺少csrf_token
在html中<head></head>标签中添加
<script>
var csrf_token ="{{csrf_token() }}";
$.ajaxSetup({
beforeSend:function(xhr, settings) {
if (!/^(GET|HEAD|OPTIONS|TRACE)$/i.test(settings.type) && !this.crossDomain) {
xhr.setRequestHeader("X-CSRFToken",csrf_token);
}
}
});
</script>