Laravel 资源控制器 ajax 调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25292942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel resource controller ajax call
提问by flyingL123
Using Laravel 4.2, I have a resource controller setup like this:
使用 Laravel 4.2,我有一个这样的资源控制器设置:
Route::resource('faq', 'ProductFaqController');
When I hit the /faq
URL on my server, the controller's index()
method correctly loads.
当我点击/faq
服务器上的URL 时,控制器的index()
方法会正确加载。
In a js file, I would like to make an ajax call to one of the other actions for this resource controller, for example, the store
action. Therefore, I do something like this:
在 js 文件中,我想对此资源控制器的其他操作之一进行 ajax 调用,例如,store
操作。因此,我做这样的事情:
$.ajax({
url:'store',
dataType:'json',
success:function(data){
console.log(data);
}
});
The problem I'm having is that since the URL is /faq
(without a trailing slash), the ajax call gets made to /store
, instead of /faq/store
.
我遇到的问题是,由于 URL 是/faq
(没有尾部斜杠),因此 ajax 调用被执行到/store
,而不是/faq/store
.
Am I missing something here or do I need to prefix the url for all of my ajax calls with the route?
我在这里遗漏了什么,还是我需要为所有 ajax 调用的 url 加上路由前缀?
If I do need to add some sort of prefix, how do I get the appropriate prefix to use no matter resource controller is being called?
如果我确实需要添加某种前缀,无论正在调用资源控制器,我如何获得适当的前缀来使用?
回答by Jeff Lambert
Run this from your command line, it will list all URL routes coupled together with the controller methods they are attached to:
从命令行运行它,它将列出所有 URL 路由以及它们所附加的控制器方法:
$ php artisan routes # Laravel 4
$ php artisan route:list # Laravel 5
Making a resource controller as you are creates 7 different routes. None of those routes include store
anywhere in the URL. The store
method exists on the controller, but responds to POST requests to the URL /faq
(in your example)
按原样制作资源控制器会创建 7 条不同的路由。这些路由store
都没有包含在 URL 中的任何位置。该store
方法存在于控制器上,但响应对 URL 的 POST 请求/faq
(在您的示例中)
The solution to thisproblem
这个问题的解决方案
Change your jQuery to POST instead of GET your data (which is default), and change your AJAX url to '/faq'
. It should then hit the correct route at that point.
将您的 jQuery 更改为 POST 而不是 GET 您的数据(这是默认设置),并将您的 AJAX url 更改为'/faq'
. 然后它应该在那个点击中正确的路线。
As far as being able to hit a resource controller route without knowledge of the actual route, that on its face is impossible (think about trying to visit google.com without knowing that google.com exists). I think however I understand what you're trying to do. I'm assuming that the AJAX request is being sent off as a response to some user action, such as the clicking of a button. You can attach a data attribute to that button with the value you're wanting so that you can make these requests in a sort-of resource agnostic way.
至于能够在不知道实际路由的情况下访问资源控制器路由,从表面上看是不可能的(想想在不知道 google.com 存在的情况下尝试访问 google.com)。不过我想我明白你想要做什么。我假设 AJAX 请求是作为对某些用户操作(例如单击按钮)的响应而发送的。您可以使用您想要的值将数据属性附加到该按钮,以便您可以以某种资源不可知的方式发出这些请求。
Something like this:
像这样的东西:
<button class="test" data-resource="faq">Click me!</button>
$('.test').on('click', function(e) {
var resource = $(this).data("resource");
$.ajax({
url: '/' + resource,
dataType:'json',
type: 'post',
success:function(data){
console.log(data);
}
});
});
回答by flyingL123
The accepted answer doesn't answer my question, maybe because I asked it poorly. The point of my question is that I want to take advantage of Laravel's route naming functionality so that I don't have to hardcode the word "faq" into all of my JS files. If the name of that controller changes, then I would need to make a change in every js file. After doing some more research, I have found this library to be what I was looking for. I haven't tried implementing it yet, but it seems to accomplish what I need: https://github.com/fedeisas/laravel-js-routes.
接受的答案没有回答我的问题,也许是因为我问得不好。我的问题的重点是我想利用 Laravel 的路由命名功能,这样我就不必将“faq”这个词硬编码到我的所有 JS 文件中。如果该控制器的名称发生变化,那么我需要在每个 js 文件中进行更改。在做了更多的研究之后,我发现这个库正是我想要的。我还没有尝试实现它,但它似乎完成了我需要的:https: //github.com/fedeisas/laravel-js-routes。