Laravel 5.1 ajax url参数是url [英] Laravel 5.1 ajax url parameter is url

查看:123
本文介绍了Laravel 5.1 ajax url参数是url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次使用Laravel 5.1,但我不明白为什么我会在ajax调用中获得404,该404将URL作为参数传递给服务器PHP脚本.

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.

我正在执行由路由处理的Ajax调用,如下所示:

I am executing an Ajax call that is being handled by a route as follows:

Route::get('ajax/{act}', ['uses' => 'AjaxController@helpers', 'as' => 'ajax.helpers']);

我希望变量{act}保持我传递的键/值对的形式.我在服务器端的PHP中对它们进行解码. Ajax PHP脚本包含各种助手,我不想为每个助手创建Laravel溃败.

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.

在我的应用中,用户将在一个表单字段中输入一个url,我将其捕获到名为website

In my app, the user will input a url in a form field, which I capture in a variable called website

我的ajax呼叫需要接受:

My ajax call needs to accept:

var url = '/ajax/act=url&u=' + website;

我这样做是为了构建URL,然后将其传递给jQuery $.getJSON调用:

I am doing this to build the url I then pass to a jQuery $.getJSON call:

var url = '/ajax/act=url&&u=' + encodeURIComponent(website);

我希望encodeURIcompponent()函数能够完成此工作,但是当任何参数中的任何参数在encodeURIComponent()之前包含/字符时,它都会返回404.我的基本url可以完美地工作,而无需使用其他url作为参数.

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.

但是将url作为变量值传递时,它将抛出404.

But passing a url as a variable value, it throws 404.

这是ajax调用中的URL返回404的样子:

This is what the url in ajax call looks like that returns 404:

http://my.app/ajax/act=url&u=http%3A%2F%2Fgoogle.com

此网址可以正常运行(我已从http://google.com中删除了//:

This url works perfectly (I have removed the // from http://google.com:

http://my.app/ajax/act=url&u=http%3Agoogle.com

当变量url中包含其他路径项时,它也会失败,因为它包含其他/字符,如下所示:

It also fails when there is additional path items in the variable url as it contains additional / characters, like as follows:

http://google.com/subfolder

如何在ajax调用中将完整的URL作为参数传递?谢谢!

How do I pass the full url as a parameter in the ajax call? Thanks!

推荐答案

我认为您在混淆路由参数和查询参数.您的路线定义为ajax/{action}.在这种情况下,{action}是一个路由参数,但是您试图将查询参数填充到其中.

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.

例如,如果您访问URL http://my.app/ajax/act=url&u=google.com,则此操作将起作用,因为您已经打到了ajax/{action}路由,其中​​{action}act=url&u=google.com.这就是将传递给您的AjaxController@helpers函数的值.但是,由于此数据作为路由参数传递,因此不在请求输入中. $request->all()将为空.

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/act=url&u=http://google.com,则此操作将无效,因为您尚未定义此路由.这并不映射到ajax/{action}路线;该路由将映射到您尚未定义的ajax/{action}//google.com(因此为404).

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

我认为您真正要寻找的是:http://my.app/ajax/url?u=http%3A%2F%2Fgoogle.com.这将以url作为{action}路由参数命中ajax/{action}路由,并且url值将位于查询参数中.在AjaxController@helpers函数内部,您可以通过$request->input('u');访问该URL.

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

如果您确实需要将这些数据作为路由参数输入,则必须确保另一种选择是确保您的路由参数使用了所有内容,包括斜杠:

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', '.*');

但是,如果执行此操作,则此路由将捕获http://my.app/ajax/...下的所有内容.

If you do this, however, this route will catch everything that falls under http://my.app/ajax/....

这篇关于Laravel 5.1 ajax url参数是url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆