引入文件:
<script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
form 表单:
<form action="" method="post" onsubmit="return false;" id="form">
{{csrf_field()}}
<input type="text" title="" name="number"/>
<input type="password" title="" name="password"/>
<input type="submit" onclick="test()"/>
</form>
js 代码:
<script>
function test(){
str = $("#form").serialize(); //序列化表单的值
$.post('{{url('/asd')}}',{"_token": "{{csrf_token()}}",str:str},function (data) {
if (data === "1"){
alert("yes")
} else {
alert("no");
}
})
}
</script>
controlelr 代码:
public function test(){
parse_str($_POST['str'],$data);
unset($data['_token']);
$result = DB::table('user')->insert($data);
if ($result){
return "1";
}else{
return "0";
}
}