javascript - Laravel 5.4 上的 Ajax 错误

标签 javascript ajax laravel

我收到这个错误

"{"status":"error","msg":"类别未创建"}"

这是我执行操作的 Controller 函数:

    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);
            }
        }

我收到此错误"{"status":"error","msg":"类别未创建"}"

这是我执行操作的 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>

我收到此错误“{”status”:“error”,“msg”:“类别未创建”}”

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>

最佳答案

我解决了问题:)

这是我执行操作的 Controller 函数:

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>

关于javascript - Laravel 5.4 上的 Ajax 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48184236/

相关文章:

javascript - Angular JS ng-app 出现在多个地方

javascript - CSS 预加载在 Google Chrome 之外不起作用

laravel - 在lumen laravel中使用stripe-connect,安装问题

linux - 在 linux mint 上安装 laravel

php - Doctrine\DBAL\Driver\PDOException SQLSTATE[HY000] [2006] MySQL 服务器已消失

javascript - 将 Promise 处理函数绑定(bind)到对象

javascript - 使用数组推送获取额外的括号

javascript - Jquery Ajax 调用返回 403 状态

javascript - 避免同步 ajax 请求并显示加载

asp.net - 如何获取在 Asp.net 中与 $.post 一起发布的整个 JSon 文件