laravel 使用 AJAX 500上传图片内部服务器错误

r6vfmomb  于 2023-03-13  发布在  其他
关注(0)|答案(3)|浏览(152)

我试图上传图像 AJAX 在laravel 5.2.but我仍然得到错误500内部服务器错误在路由.当我试图上传图像使用ajax请求浏览器显示正确的路由路径,但我仍然没有得到为什么它仍然显示错误给我的原因.

超文本标记语言

<!-- CHANGE AVATAR TAB -->
                        <div class="tab-pane" id="tab_1_2">

                            <div class="uploadimagediv"></div> 
                                {{ Form::open(array('url' => 'admin/avatar','method'=>'post','files'=>'true','id'=>'updateprofileimage'))  }}

                                <div class="form-group">
                                    <div class="fileinput fileinput-new" data-provides="fileinput">
                                        <div class="fileinput-new thumbnail" style="width: 200px; height: 150px;">
                                            <img src="http://www.placehold.it/200x150/EFEFEF/AAAAAA&amp;text=no+image" alt=""/>
                                        </div>
                                        <div class="fileinput-preview fileinput-exists thumbnail" style="max-width: 200px; max-height: 150px;">
                                        </div>
                                        <div>
                                            <span class="btn default btn-file">
                                            <span class="fileinput-new">
                                            Select image </span>
                                            <span class="fileinput-exists">
                                            Change </span>
                                            <p class="text-danger" id="error_image"></p>
                                            <input type="file" id="picture" name="picture"/>
                                            {{--{{ Form::file('picture') }}--}}
                                            </span>
                                            <span class="error alert-danger">{{ $errors->first('picture') }}</span>
                                            <a href="javascript:;" class="btn default fileinput-exists" data-dismiss="fileinput">
                                            Remove </a>
                                        </div>
                                    </div>
                                    <div class="clearfix margin-top-10">

                                    </div>
                                </div>
                                <div class="margin-top-10">
                                    {{Form::hidden('id', 2, ["id"=>"id"])}}
                                    {{ Form::button('Upload',['id'=> 'updatepicture','class'=>'btn green-haze']) }}

                                    {{--{{ Form::submit('Submit',['class' => 'btn green-haze','name'=>'changeImage']) }}--}}
                                    <a href="javascript:;" class="btn default">
                                    Cancel </a>
                                </div>
                            {{ Form::close() }}
                        </div>
                        <!-- END CHANGE AVATAR TAB -->

路线

Route::group(['prefix' => 'admin'], function ()
{
    Route::controller('/','DashboardController');
});

** AJAX **

$(document).on('click', '#updatepicture', function($e)
{
    $e.preventDefault();
    // send an ajax request
    $("#error_image").empty();
    $.ajax(
    {

        url: 'avatar',
        processData: false,
        contentType: false,
        type: "post",//use for update
        data: new FormData($("#updateprofileimage")[0]),
        success: function(data)
        {
            if(data.status)
            {           
                $("#uploadimagediv").html('<div class="alert alert-success"><button type="button" class="close">×</button>'+data.message+'</div>');
                window.setTimeout(function()
                {
                    $(".alert").fadeTo(500, 0).slideUp(500, function()
                    {
                        $(this).remove(); 
                    });
                }, 5000);
                $('.alert .close').on("click", function(e)
                {
                    $(this).parent().fadeTo(500, 0).slideUp(500);
                });
                //console.log(data);
                //$("#updateprofileimage")[0].reset();
                //window.location.href = "http://localhost/laravel/admin/profile";
            }
            else
            {
                errors = data.errors;

                for(error in errors)
                {
                    $("#error_"+error.title).html(error.message);
                }
            }
        },
        error: function(xhr)
        {
            if(xhr.status == 422)
            {
                errors = xhr.responseJSON;
                for(error in errors)
                {
                    $("#error_"+error).html(errors[error][0]);
                }
            }
        }
    });
});

错误为:“网络错误:500内部服务器错误-http://localhost/laravel/admin/avatar
请指出我哪里错了。
控制器是

public function postAvatar(ImageUploadRequest $request)
{
  ---
}
atmip9wb

atmip9wb1#

Add the below line inside <head>
<meta name="csrf-token" content="{{ csrf_token() }}">

And add the below lines before your ajax call in javascript function
$.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
});

And don't forget to give permission to your storage folder
sudo chmod -R 777 storage
d7v8vwbk

d7v8vwbk2#

在你 AJAX 设置中,你必须在请求中提供x-csrf-token。对于你的ajax请求,你的url也有问题:

$(document).on('click', '#updatepicture', function($e)
    {
        $e.preventDefault();
        // send an ajax request
        $("#error_image").empty();
        $.ajax(
        {

            url: "{{url('avatar')}}",
            processData: false,
            contentType: false,
            type: "post",//use for update
            data: new FormData($("#updateprofileimage")[0]),
            headers: {
            'X-CSRF-TOKEN': "{{ csrf_token() }}"
            },
            success: function(data)
            {
                if(data.status)
                {           
                    $("#uploadimagediv").html('<div class="alert alert-success"><button type="button" class="close">×</button>'+data.message+'</div>');
                    window.setTimeout(function()
                    {
                        $(".alert").fadeTo(500, 0).slideUp(500, function()
                        {
                            $(this).remove(); 
                        });
                    }, 5000);
                    $('.alert .close').on("click", function(e)
                    {
                        $(this).parent().fadeTo(500, 0).slideUp(500);
                    });
                    //console.log(data);
                    //$("#updateprofileimage")[0].reset();
                    //window.location.href = "http://localhost/laravel/admin/profile";
                }
                else
                {
                    errors = data.errors;

                    for(error in errors)
                    {
                        $("#error_"+error.title).html(error.message);
                    }
                }
            },
            error: function(xhr)
            {
                if(xhr.status == 422)
                {
                    errors = xhr.responseJSON;
                    for(error in errors)
                    {
                        $("#error_"+error).html(errors[error][0]);
                    }
                }
            }
        });
    });
d7v8vwbk

d7v8vwbk3#

任何有500服务器错误的人,记得在php.ini(xamp的)中打开phpinfo,在cpanel主机上,进入php选择版本-〉扩展-〉检查phpinfo是否正常

相关问题