I am working with Laravel 5.1 for the first time and I cannot understand why I am getting 404s on an ajax call that passes a URL to the server PHP script as a parameter.
我第一次使用Laravel 5.1,我无法理解为什么我要在ajax调用中获得404s,该调用将URL作为参数传递给服务器PHP脚本。
I am executing an Ajax call that is being handled by a route as follows:
我正在执行的Ajax调用由以下路径处理:
Route::get('ajax/{act}', ['uses' => 'AjaxController@helpers', 'as' => 'ajax.helpers']);
I want the variable {act}
to hold the sring of key / value pairs I pass. I decode these in the PHP at the server end. The Ajax PHP script contains a variety of helpers and I do not want to create a Laravel rout for each.
我希望变量{act}包含我传递的键/值对。我在服务器端的PHP中解码这些。Ajax PHP脚本包含各种助手,我不想为每个助手创建一个Laravel路由器。
In my app, the user will input a url in a form field, which I capture in a variable called website
在我的应用程序中,用户将在表单字段中输入url,我将在一个名为website的变量中捕获该字段
My ajax call needs to accept:
我的ajax电话需要接受:
var url = '/ajax/act=url&u=' + website;
I am doing this to build the url I then pass to a jQuery $.getJSON
call:
我这样做是为了构建url,然后将url传递给jQuery $。getJSON电话:
var url = '/ajax/act=url&&u=' + encodeURIComponent(website);
I would expect the encodeURIcompponent()
function to make this work, but it returns 404
when any of the parameters contain /
characters prior to the encodeURIComponent()
. My base url works perfectly without the additional url as a parameter.
我希望使用encodeURIcompponent()函数来完成这项工作,但是在encodeURIComponent()之前的任何参数包含/字符时,它都会返回404。我的基本url在没有附加url作为参数的情况下工作得很好。
But passing a url as a variable value, it throws 404.
但将url作为变量值传递时,会抛出404。
This is what the url in ajax call looks like that returns 404:
这就是ajax调用中的url返回404的样子:
http://my.app/ajax/act=url&u=http%3A%2F%2Fgoogle.com
This url works perfectly (I have removed the //
from http://google.com
:
这个url非常好用(我已经从http://google.com删除了//)。
http://my.app/ajax/act=url&u=http%3Agoogle.com
It also fails when there is additional path items in the variable url as it contains additional /
characters, like as follows:
当变量url中有额外的路径项时,它也会失败,因为它包含了额外的/字符,如下所示:
http://google.com/subfolder
How do I pass the full url as a parameter in the ajax call? Thanks!
如何将完整url作为ajax调用中的参数传递?谢谢!
2 个解决方案
#1
3
I think you're confusing route parameters and query parameters. Your route is defined as ajax/{action}
. In this case, {action}
is a route parameter, but you're trying to stuff query parameters into it.
我认为您混淆了路由参数和查询参数。您的路由被定义为ajax/{action}。在本例中,{action}是一个route参数,但您试图将查询参数填充到其中。
For example, if you access the url http://my.app/ajax/act=url&u=google.com
, this will work because you've hit the route ajax/{action}
, where {action}
is act=url&u=google.com
. That is the value that will get passed to your AjaxController@helpers
function. However, since this data is passed in as a route parameter, it is not in the request input. $request->all()
will be empty.
例如,如果您访问url http://my.app/ajax/actt = url&ui =google.com,这将有效,因为您访问了ajax/{动作}路径,其中{动作}为act=url&u=google.com。这是传递给ajaxcontroller@helper函数的值。但是,由于该数据作为路由参数传入,所以它不在请求输入中。请求- >所有美元()将是空的。
However, if you access the url http://my.app/ajax/act=url&u=http://google.com
, this will not work, as you do not have this route defined. This does not map to the ajax/{action}
route; this route would be mapped to ajax/{action}//google.com
, which you do not have defined (hence the 404).
但是,如果您访问url http://my.app/ajax/act= url&ui =http://google。这不会映射到ajax/{action}路径;该路径将映射到ajax/{action}/ google.com,您还没有定义它(因此是404)。
I think what you're really looking for is this: http://my.app/ajax/url?u=http%3A%2F%2Fgoogle.com
. This will hit your ajax/{action}
route with url
as the {action}
route parameter and the url value will be in the query parameters. Inside your AjaxController@helpers
function, you can access the url via $request->input('u');
.
我想你真正想要的是:http://my.app/ajax/url?这将以url作为{action}路由参数命中ajax/{action}路由,url值将在查询参数中。在ajaxcontroller@helper函数中,可以通过$request->输入('u')访问url。
Edit
If you really need this data to come in as a route parameter, another option you have to make sure your route parameter consumes everything, including slashes:
如果您确实需要这些数据作为路由参数输入,那么您必须确保您的路由参数使用所有内容,包括斜线:
Route::get('ajax/{action}', ['uses' => 'AjaxController@helpers', 'as' => 'ajax.helpers'])
->where('action', '.*');
If you do this, however, this route will catch everything that falls under http://my.app/ajax/...
.
但是,如果您这样做,该路径将捕获属于http://my.app/ajax/…的所有内容。
#2
0
You are using relative paths with your url variable (as you've a leading /
in your URL), that could be causing the 404
not found error.
您正在使用url变量的相对路径(就像您的url中有一个引导/),这可能导致404没有找到错误。
Try storing your base url
using Laravel's helper method url()
in a hidden field in your view (you could do it in your master view if you wanted).
尝试使用Laravel的helper方法url()将基本url存储在视图中的一个隐藏字段中(如果需要,可以在主视图中进行)。
<input id='baseUrl' type='hidden' value='{{ url() }}' />
You can grab the value as simple as creating a JS helper function as such:
您可以简单地获取值,创建一个JS助手函数,如下所示:
var baseUrl = function(){
return $('#baseUrl').val();
};
Then append that url into the beginning of your variable url
like so:
然后将该url附加到变量url的开头,如下所示:
var url = baseUrl() + '/ajax/act=url&&u=' + encodeURIComponent(website);
#1
3
I think you're confusing route parameters and query parameters. Your route is defined as ajax/{action}
. In this case, {action}
is a route parameter, but you're trying to stuff query parameters into it.
我认为您混淆了路由参数和查询参数。您的路由被定义为ajax/{action}。在本例中,{action}是一个route参数,但您试图将查询参数填充到其中。
For example, if you access the url http://my.app/ajax/act=url&u=google.com
, this will work because you've hit the route ajax/{action}
, where {action}
is act=url&u=google.com
. That is the value that will get passed to your AjaxController@helpers
function. However, since this data is passed in as a route parameter, it is not in the request input. $request->all()
will be empty.
例如,如果您访问url http://my.app/ajax/actt = url&ui =google.com,这将有效,因为您访问了ajax/{动作}路径,其中{动作}为act=url&u=google.com。这是传递给ajaxcontroller@helper函数的值。但是,由于该数据作为路由参数传入,所以它不在请求输入中。请求- >所有美元()将是空的。
However, if you access the url http://my.app/ajax/act=url&u=http://google.com
, this will not work, as you do not have this route defined. This does not map to the ajax/{action}
route; this route would be mapped to ajax/{action}//google.com
, which you do not have defined (hence the 404).
但是,如果您访问url http://my.app/ajax/act= url&ui =http://google。这不会映射到ajax/{action}路径;该路径将映射到ajax/{action}/ google.com,您还没有定义它(因此是404)。
I think what you're really looking for is this: http://my.app/ajax/url?u=http%3A%2F%2Fgoogle.com
. This will hit your ajax/{action}
route with url
as the {action}
route parameter and the url value will be in the query parameters. Inside your AjaxController@helpers
function, you can access the url via $request->input('u');
.
我想你真正想要的是:http://my.app/ajax/url?这将以url作为{action}路由参数命中ajax/{action}路由,url值将在查询参数中。在ajaxcontroller@helper函数中,可以通过$request->输入('u')访问url。
Edit
If you really need this data to come in as a route parameter, another option you have to make sure your route parameter consumes everything, including slashes:
如果您确实需要这些数据作为路由参数输入,那么您必须确保您的路由参数使用所有内容,包括斜线:
Route::get('ajax/{action}', ['uses' => 'AjaxController@helpers', 'as' => 'ajax.helpers'])
->where('action', '.*');
If you do this, however, this route will catch everything that falls under http://my.app/ajax/...
.
但是,如果您这样做,该路径将捕获属于http://my.app/ajax/…的所有内容。
#2
0
You are using relative paths with your url variable (as you've a leading /
in your URL), that could be causing the 404
not found error.
您正在使用url变量的相对路径(就像您的url中有一个引导/),这可能导致404没有找到错误。
Try storing your base url
using Laravel's helper method url()
in a hidden field in your view (you could do it in your master view if you wanted).
尝试使用Laravel的helper方法url()将基本url存储在视图中的一个隐藏字段中(如果需要,可以在主视图中进行)。
<input id='baseUrl' type='hidden' value='{{ url() }}' />
You can grab the value as simple as creating a JS helper function as such:
您可以简单地获取值,创建一个JS助手函数,如下所示:
var baseUrl = function(){
return $('#baseUrl').val();
};
Then append that url into the beginning of your variable url
like so:
然后将该url附加到变量url的开头,如下所示:
var url = baseUrl() + '/ajax/act=url&&u=' + encodeURIComponent(website);