简体   繁体   English

我正在通过 Laravel 中的 AJAX 上传图片,已成功上传但成功功能不起作用

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

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.这是我的 ajax 脚本,它在 MySQL 数据库中成功上传图像,但成功消息没有显示警报或不工作,下面是我的存储方法:我已经看到了其他答案,但没有任何其他答案,例如删除数据类型: JSON,任何人都可以在下面查看我的代码并找出错误。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM