hanif-king's avatar

Method Not Allowed exception on ajax file upload

hi , i am quite confused of this error i cant see any error in my code but i get method not allowed error after the lick event of button upload. i am using ajax request to upload an image when i faced this problem. here is my controller code

 public function UploadImage(Request $request){
        if (Input::hasFile('image'))
        {
            return "file present";
        }
        else{
            return "file not present";
        }
    }

here is my route

       route::post('AjaxUpload', 'AjaxUploadController@UploadImage')->name('AjaxUpload');

here is my view

<form enctype="multipart/form-data" id="upload_form" role="form" method="POST" action="">
        <div class="form-group" >
            <button class=" addbtn bg_db control_UPL" title="upload Image">UPL</button>
            <input type="hidden" name="_token" value="{{ csrf_token()}}">
            <input type="file" id="catagry_logo" name="image" class="form-control-file">
            <div class="AjContent">
               <img class="looper" src="{{URL::to('admin/images/ajax/looper.gif')}}">
                <span class="ajaxData"></span>
            </div>
        </div>
    </form>

here is my Ajax request code

<script type="text/javascript">
    document.addEventListener('DOMContentLoaded', function(){

        $(".addbtn").click(function(){
            $.ajax({
                url:"{{route('AjaxUpload')}}",
                data:new FormData($("#upload_form")[0]),
                dataType:'json',
                async:false,
                type:'post',
                processData: false,
                contentType: false,
                success:function(response){
                    console.log(response);
                }
            });
        })
    }, false);


</script>

i am getting the error method not allowed on button click event. please some help. thank you.

0 likes
6 replies
Cronix's avatar
Cronix
Best Answer
Level 67

Is that js in a standalone .js file, or in a blade view?

hanif-king's avatar

oh i wanted to like your response but licked on solved. my question is not solved yes please i need help. @Cronix

hanif-king's avatar

@jlrdw i saw the link you mention. they are just on posting texts not images. my case is on how to upload image as ajax request in same project right now i am working. i have many other ajax requests where none of them has input of type file. and works properly without error. it's ambiguous for me on how to upload image as ajax request . thank you

Please or to participate in this conversation.