[英]Ajax Error On Laravel 5.4
I got this error 我得到这个错误
"{"status":"error","msg":"Category did not created"}"
Here is my Controller Function in which i perform action : 这是我执行操作的控制器功能:
function create_category(Request $request){
if($request->ajax()){
$category_name = $request->input('create_category');
DB::table('tbl_smscategories')->insert($category_name);
$response = array(
'status' => 'success',
'msg' => 'Category created successfully',
);
return Response::json($response);
}else{
$response = array(
'status' => 'error',
'msg' => 'Category did not created',
);
return Response::json($response);
}
}
I got this error "{"status":"error","msg":"Category did not created"}"
我收到此错误"{"status":"error","msg":"Category did not created"}"
Here is my ajax Code in which i perform action : 这是我执行操作的ajax代码:
<script type = "text/javascript">
$('#add-order').click(function(e) {
e.preventDefault();
//setting variables based on the input fields
var inputcreate_category = $('input[name="create_category"]').val();
var token = $('input[name="_token"]').val();
var data = {
create_category: inputcreate_category,
token: token
};
var request = $.ajax({
url: "/create-category",
type: "POST",
data: data,
dataType: "html",
});
request.done(function(msg) {
var response = JSON.parse(msg);
console.log(response.msg);
});
request.fail(function(jqXHR, textStatus) {
console.log("Request failed: " + testStatus);
});
}); < /script>
I got this error "{"status":"error","msg":"Category did not created"}" 我收到此错误“ {” status“:” error“,” msg“:”类别未创建“}”
Html Element : HTML元素:
<form method="post" action=" {{ route('createcategory') }} " enctype="multipart/form-data" method="post">
{{ csrf_field() }}
<div class="form-group">
<label>Category Name</label>
<input type="text" name="create_category" id="create_category" class="form-control" placeholder="Enter Category Name">
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary" id="create">Create</button>
</div>
</form>
I Solved the problem :) 我解决了问题:)
Here is my Controller Function in which i perform action : 这是我执行操作的控制器功能:
function create_category(Request $request){
if($request->ajax()){
$category_name = $request->input('create_category');
$data = array(
'cat_name' => $category_name,
'shop_id' => 1,
);
DB::table('tbl_smscategories')
->insert($data);
$response = array(
'status' => 'success',
'msg' => 'Category created successfully',
);
return json_encode($response);
}else{
$response = array(
'status' => 'error',
'msg' => 'Category did not created',
);
return json_encode($response);
}
}
Here is my ajax Code in which i perform action :
<script type = "text/javascript">
/*Code For Creating Category*/
$(document).ready(function(){
$('.cat_form').submit(function(e){
e.preventDefault();
$.ajax({
url:$(this).attr('action'),
type:"POST",
data:$(this).serialize(),
success:function(result){
json_format = $.parseJSON(result);
alert(json_format.msg);
},
error:function(xhr, status){
json_format = $.parseJSON(result);
alert(json_format.msg);
}
})
})
})
< /script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.