返回一个函数的变量结果的Ajax响应

返回一个函数的变量结果的Ajax响应

问题描述:

这是我的Ajax调用:返回一个函数的变量结果的Ajax响应

$("#cover-input").change(function(){ 
var file_data = $("#cover-input").prop("files")[0]; 
var form_data = new FormData(); 
form_data.append("cover_file", file_data); 
//kaherdin 
$.ajax({ 
    url: 'update-cover', 
    type: 'POST', 
    dataType: 'script', 
    data: form_data, 
    contentType: false, 
    processData: false, 
    async: false, 
    success: function(resp){ 
     console.log(resp); 
    }, 
    error: function(err){ 
     console.log('Likes error', err); 
    } 
}); 
    readURL_cover(this); 
}); 

了我在basicly修剪和上载变更文件的功能。

public function updateCover(Request $request) { 
    $user = Sentinel::check(); 
    $destinationPath = public_path('uploads/users'); 

    if ($fileCover = $request->file('cover_file')) { 
     $input_cover = time().'.'.$fileCover->getClientOriginalExtension(); 
     $img = Image::make($fileCover->getRealPath()); 
     $img->fit(1920, 555, function ($constraint) { 
      $constraint->aspectRatio(); 
     })->save($destinationPath.'/'.$input_cover); 
     // $user->cover = $input_cover; 

     $response = $input_cover;   
     return $response; 
    } 

但是,这给我一个错误。我只想让“input_cover”返回到我的ajax调用,以便我可以显示更新后的图片。 如果我将$ response = $ input_cover更改为$ response = [$ input_cover];它工作纠结,但输入是像:[“my_pic.jpg”]所以它不好。

+0

什么错误?你永远不会在PHP脚本中调用该函数。 –

+0

喜欢错误对象{readyState的:4,getResponseHeader:功能,getAllResponseHeaders:功能,setRequestHeader:功能,overrideMimeType:函数...} 呼叫在这里做:$。阿贾克斯({ 网址: '更新盖', – Kaherdin

+0

_but这给我一个错误_你想与我们分享你得到什么错误,或者我们应该做一些猜测吗? – RiggsFolly

您应该返回JsonResponse像这样:

return response()->json(['input_cover' => $input_cover]); 

检查这在JSON响应以及它们是如何工作https://laravel.com/docs/5.4/responses#json-responses

+0

好!如果我只需要我的变量的名称?我可以在JS中做到这一点,它是更好的方法? – Kaherdin