i am uploading Image through AJAX in laravel, which is successfully uploaded but Successfull fucntion is not working

Viewed 37

this is my ajax script which is uploading the image successfully in MySQL database but the success message is not showing alert or not working, and below is my store method: I have seen other answers but is not working any other answer, like removing datatype: JSON, anyone reviews my code below and sorts out the mistake.

View file:

   $('#party_create_form').on('submit', function(e){
    e.preventDefault();
    $.ajax({
        url: '{{route('party.store')}}',
        method: 'POST',
        data:new FormData(this),
        dataType: "JSON",
        contentType: false,
        cache: false,
        processData: false,
        headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
        success:function(data) 
        {
            alert('hi');

        }
    })
});

Controller File:

public function store(PartyRequest $request)
{
  $validator = Validator::make($request->all(), [
    'name' => 'required|unique:parties',
    'party_logo' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
  ]);

    if ($validator->passes()) {
    
    $input['party_logo'] = time().'.'.$request->party_logo->extension();
    
    $request->party_logo->move(public_path('party_logo'), $input['party_logo']);
    
    $data = ['name' => $request->name,'party_logo'=>$input['party_logo']];
    
    Party::create($data);
    return response()->json();

  } else {

    return response()->json();
  }
}
1 Answers

Try this

public function store(PartyRequest $request)
{
    $validator = Validator::make($request->all(), [
      'name' => 'required|unique:parties',
      'party_logo' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
    ]);

    if ($validator->passes()) {
    
      $input['party_logo'] = time().'.'.$request->party_logo->extension();
      
      $request->party_logo->move(public_path('party_logo'), $input['party_logo']);
      
      $data = ['name' => $request->name,'party_logo'=>$input['party_logo']];
      
      Party::create($data);
      $msg = "success";
      $array =  array("status" => 200, "msg" => $msg, "result" => array());

    } else {

      $msg = "Failed";
      $array =  array("status" => 400, "msg" => $msg, "result" => array());

    }
    return \Response::json($array);
}
Related