2017-02-14 34 views
0

我测试了一些简单的Ajax请求,我收到错误代码500,而试图与响应控制器 内部服务器错误500简单的jQuery Ajax请求Laravel ..错误代码:500

我web.php文件

This one works fine 
//Route::get('/test', function() { 
// return "i am a success get"; 
//}); 

I cannot seem to get this one to work 
Route::get('/test', '[email protected]'); 

我的控制器

class shoppingCartController extends Controller 
{ 
    public function testfunction(Illuminate\Http\Request $request) 
    { 
//   
//  if ($request->isMethod('post')){  
//   return response()->json(['response' => 'This is post method']); 
//  } 
// 
//  return response()->json(['response' => 'This is get method']); 
//   
     return 'test'; 
    } 
} 

而且我的jQuery

$(document).ready(function(){ 

    $("button").click(function(){ 
     $.ajax({url: "test", success: function(result){ 
       $("#div1").html(result); 
      }, error: function(xhr){ 
       $("#div1").html(xhr.statusText + " " + xhr.status); 
      } 
     }); 
    }); 
}); 
+1

它工作没有ajax? – n00dl3

+0

你的AJAX调用使用什么方法?如果它使用'POST',那么你会得到一个错误。如果您在浏览器中使用开发工具,您将能够看到实际的错误消息。 – James

+0

使用萤火虫看到实际的错误讯息,并在这里发布。在ajax url上添加'/'使其成为'/ test'。它工作吗? –

回答

0

好的答案是n00dl3建议控制器没有工作没有ajax也。看来我是越来越

Illuminate\Http\Request not found exception 
0

使用此代码Ajax调用:

<script type="text/javascript"> 
    var urla = {!! json_encode(url('/')) !!}; 
    jQuery.ajax({ 
      type: "GET", 
      url: urla + '/test', 
      dataType: 'json', 
      data: {logtype: type}, 
      success: function (msg) { 
       console.log(msg); 
      }, 
     }); 
</script> 

,并设置 '测试' 的方法:

Route::get('/test', '[email protected]'); 

这里改变TestController到控制器哪里你已经把test方法。

Ofcourse可以在ajax调用中添加错误子句。这里我只是加了console.log(msg)其中msg是json编码的响应。