【问题标题】:Internal server error 500 Laravel 5.4 AJAX内部服务器错误 500 Laravel 5.4 AJAX
【发布时间】:2019-05-12 01:00:42
【问题描述】:

我正在使用 laravel 5.4,我想创建可以将数据发布到服务器的 AJAX,但我收到了这个错误消息

加载资源失败:服务器响应状态为 500(内部服务器错误)

https://i.stack.imgur.com/gjIbU.png

这是我的 AJAX

$('#testAjax').on('click',function(){
$.post('{{ route('edit') }}',{body:'string',_token:'{{ Session::token() }}'},function(data){
    console.log(JSON.stringify(data));
  });
});

我的路线

Route::post('/edit',[ 'uses'=>'AjaxController@getProfessions', 'as'=>'edit']);

我的控制器

public function getProfessions(Request $request)
{
  $this->validate($request, [
        'body' => 'required'
    ]);
    $p = profession::where('categories_id'=>$request['postId']);
    return response()->json(['new_body' => 'Server'], 200);
}

目前我只想从服务器响应“服务器”而不是“字符串” ajax 它自己,所以我知道它来自服务器

【问题讨论】:

  • 嗨马丁,错误信息是什么
  • @WalterCejas 加载资源失败:服务器响应状态为 500(内部服务器错误)
  • 在 90% 的情况下,您可以在 storage/logs 中的 .log 文件中找到完整的错误、控制器和行,或者也可以通过单击网络中的请求、查看预览或响应标签..

标签: php ajax laravel


【解决方案1】:

在你的 routes/web.php

Route::post('/edit','AjaxController@getProfessions')->name('edit');

在你的控制器中:

use Illuminate\Support\Facades\Validator;

public function getProfessions(Request $request)
{


 try {
        $validator = Validator::make($request->all(), [
                'postId' => 'required',
                'body' => 'required'
            ]
        );

        if ($validator->fails()) {
            $response=array('status'=>'error','errors'=>implode(',', $validator->errors()->all()));
            return response()->json($response, 200);
        }else{
           $profession = Profession::where(['categories_id'=>$request->input('postId')])->first();
           if($profession){
             $profession->body=$request->input('body');
             $profession->save();
             return response()->json(['profession'=>$profession], 200);
           }else{
               $response=array('status'=>'error','errors'=>'profession not found');
               return response()->json($response, 200);
            }

        }
   }catch(\Exception $e){
        $response=array('status'=>'error','errors'=>'Internal Server Error');
        return response()->json($response, 500);
  }
}

在您的编辑刀片视图中,您将 $profession 传递给查看:

<meta name="csrf-token" content="{{ csrf_token() }}" />

<form>
    <input type="hidded" id="postID" name="postID" value="{{$profession->postID}}" />
    <input type="text" id="body" name="body" value="{{$profession->body}}" />
   <button type="button" id="testAjax">Submit using AJAX</button>
</form>

在你的 ajax 函数中:

$('#testAjax').on('click',function(){
     var postID=$('#postID').val();
     var body=$('#body').val();
     $.ajax({
          url:"{{ route('edit')}}",
          headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
          type:"POST",
          dataType:"json",
          data:{
              postId:postID,
              body:body,
          },
          success:function(response){
              console.log(response);
          },
          error:function(err){

          }
     });
});

【讨论】:

    【解决方案2】:

    首先,Laravel 中的良好实践表明我们应该使用大写的模型名称的第一个字母,也许这就是你的问题。尝试改变它。

    Profession::where...

    其次,你不处理错误响应,你应该在你的Javascript中添加一个函数来处理它,像这样:

    error: function(data) {
        console.log(data)
    }
    

    第三,你试图从请求中获取postId,但你没有发送它。

    请更改您的代码并让我们知道结果。

    【讨论】:

      【解决方案3】:

      你使用 $.post 不正确,试试这个:

      $.post( "{!! route('edit') !!}", { body:'string',_token: {!! Session::token() !!} })
        .done(function( data ) {
          console.log(JSON.stringify(data));
        });
      
      • 在您的日志文件(存储/日志)中检查错误 500 的文件和行。
      • 将您的逻辑代码包装到 try/catch 中以处理错误。
      • 使用带有 XDebug 的 PHPStorm 等 IDE 来调试请求,并检查请求的每个参数(如令牌和字符串)是否设置正确。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-04-12
        • 2020-02-12
        • 2018-04-16
        • 2016-03-24
        • 1970-01-01
        • 2018-03-12
        • 2015-09-20
        • 2023-03-04
        相关资源
        最近更新 更多