【问题标题】:Unable to call Laravel routes in Jquery/Ajax无法在 Jquery/Ajax 中调用 Laravel 路由
【发布时间】:2017-10-08 19:19:17
【问题描述】:

我无法在 laravel 文件夹外调用 laravel 路由。

我的路线

 Route::resource('tmrate','WebServiceController');

我的控制器功能来获取和发布

  public function store(Request $request)
{
    Teamrating::create($request->all());
        return response()->json(['response'=>'true','message'=>'Rating Submitted','code'=>201],201);

}


public function show($id)
{
    $teamratings=Teamrating::find($id);
        return response()->json(['response'=>'true','message'=>'ratings found','data'=>$teamratings,'code'=>201],201);

}

当使用邮递员访问 laravel 路由/url 时,它可以正常工作

 http://localhost/saniservices/public/tmrate/10       for get

 http://localhost/saniservices/public/tmrate?requestid=2&clientid=1&propertyid=1&servicesheetId=1&serviceTypeId=1&serviceDate=2017-05-05&rating=5&reason=Best service&comment=Great services and workd&teamId=1      for post

现在我正在使用这个代码

 <!DOCTYPE html>
 <html>

 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

 <body>

 <form  action="#" method="post" id="idForm">
    <input type="submit" value="Submit" id="submit">
 </form> 


  </body>

 <script type="text/javascript">



    $("#idForm").submit(function(){


        $.ajax({
                 url: 'http://localhost/saniservices/public/tmrate',
                 type: 'GET',
                 dataType: 'json',
                 data:{"id":1},
                 success: function (data) {
                     alert('success');

                 }

             });

      });



  </script>
  </html>

但是在这里我没有得到任何来自 api 的响应,我不知道为什么会这样,我做了很多尝试但我没有得到任何响应

以上文件放在 laravel 安装之外。

【问题讨论】:

  • 您的浏览器开发工具是否显示正在发出的请求?有什么反应?请求 URL 的参数是否正确?
  • 按方法类型 POST 提交表单。现在控制器期望它,但您仍然通过 GET 调用。

标签: php jquery ajax laravel laravel-5


【解决方案1】:

问题是,在ajax 中指定的type 是:

type: 'GET',

在 route.php 中的路由是:

Route::resource('tmrate','WebServiceController');

这里的类型是resource。将其更改为 get 并重试。

【讨论】:

  • 谢谢,我只是被蒙蔽了,因为它是如此简单明了。
猜你喜欢
  • 2016-05-26
  • 2017-03-23
  • 2019-07-10
  • 2020-03-04
  • 1970-01-01
  • 1970-01-01
  • 2021-01-27
  • 2016-01-25
  • 1970-01-01
相关资源
最近更新 更多