gpt4 book ai didi

php - 无法在 Jquery/Ajax 中调用 Laravel 路由

转载 作者:行者123 更新时间:2023-12-01 07:43:18 25 4
gpt4 key购买 nike

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

我的路线

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

我的 Controller 函数用于获取和发布

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

}

当使用 postman 点击 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 安装之外。

最佳答案

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

type: 'GET',

在route.php中,路线是:

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

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

关于php - 无法在 Jquery/Ajax 中调用 Laravel 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43887441/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com