1

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.

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

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.

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

My ajax call needs to accept:

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

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

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.

But passing a url as a variable value, it throws 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

This url works perfectly (I have removed the // from 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:

http://google.com/subfolder

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

1
  • You mean the second url that you've said works, returns some kind of response? Commented Aug 22, 2015 at 7:12

2 Answers 2

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.

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.

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

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

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

Sign up to request clarification or add additional context in comments.

5 Comments

Hi @patricus I understand. Let me try!
@TheRealPapa That encoding doesn't matter. The Laravel router decodes the url before parsing it. You can't have slashes in route parameters (encoded or not).
@TheRealPapa I have added another alternative for you to try, if you really need the data as a route parameter vs query parameter.
I just built it like your first suggestion (which makes a lot of sense) but I get an error where the code ties to assign a value from $request. The $request->input('u') fails, and $request contains an instance of a class and not any of the parameter values
@TheRealPapa That's really a new question. Can you post a new question showing the updated route and the controller code? Comment here to let me know when you've posted it so I can try to help. Also, if you feel this answer answered your original question, please mark it as such, so that future searchers can know at a glance it is helpful.
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.

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

<input id='baseUrl' type='hidden' value='{{ url() }}' />

You can grab the value as simple as creating a JS helper function as such:

var baseUrl = function(){
    return $('#baseUrl').val();
};

Then append that url into the beginning of your variable url like so:

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

1 Comment

Hi Ahmad, thanks for the reply. The url is being entered by a user in a form field. The Ajax is for checking its validity and that it meets certain format requirements. I need to pull the url from the input and pass it to a server script. Laravel does not generate the url for me. I think I may not have been clear enough in my question. I will ammend. Did I misunderstand your suggestion?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.