laravel ajax url,php – Laravel 5.1 ajax url parameter is url – Stack Overflow

  • Post author:
  • Post category: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.

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!