首页 文章

Laravel 5中使用ajax的MethodNotAllowedHttpException错误

提问于
浏览
0

我正在尝试使用ajax上传图像并且我得到:无法加载资源:服务器响应状态为500(内部服务器错误) . 这是我的ajax:

$('document').ready(function() {
    $('#uploadImage').change(function(){
        image = $('#uploadImage').val;
        token = $('#token').val();
        $.ajax ({
            type: 'POST',
            url: '/photo',
            data: { image , token },
            success: function(){

                $('.img-thumbnail').attr("src", 'images/new_image.png');
            }
        })
    })


});

这是我的路线: Route::post('/photo', 'ShopsController@uploadPhoto');

这是我的控制器:

public function uploadPhoto(Request $request)
    {

        //Sets file name according to authenticated shop id
        $imageName = 'new_image.png';

        //Save file to public/images

        $img = Image::make($request->file('image'));
     $img->resize(380, 300)->save('images/' . $imageName, 60);
    }

这是我的形式:

<form action="{{ action('ShopsController@store') }}" method="post" enctype="multipart/form-data">
    <input id="token" type="hidden" name="_token" value="{{ csrf_token() }}">
   <input id="uploadImage" class="btn btn-upload" type="file" name="image"> </form>

1 回答

  • 0

    AJAX文件上传比获取图像元素的value属性要复杂一些 .

    在这里阅读这个问题的第一个答案:jQuery Ajax File Upload

    它使用FormData对象MDN

    // HTML file input, chosen by user
    formData.append("userfile", fileInputElement.files[0]);
    

    虽然并非所有浏览器都支持它 .

    这里有一个简单的jQuery ajaxFileUpload示例:http://www.sitepoint.com/image-upload-example/

相关问题