Tutorial :How do I format the url: for a jQuery ajax() request in CakePHP so it won't add a question mark to the url?



Question:

I'm trying to call the 'tariff' action of my 'countries' controller using jQuery ajax() and pass it a country name in the following format:

/countries/tariff/countryname  

However, with the following code ( set to GET ), it is calling this with the get ? added:

/countries/tariff/?countryname  

Here's the code:

$(document).ready(function(){      $('#CountriesIndexForm select').change(function(){                    $.ajax({              type: "GET",                url: "/countries/tariff/",                data: escape($(this).val()),                success: function(html){                    $(this).parent().next('div').html(html);                }          });      });   });  

I understand its because the type is set to GET, but is there a fix for this?


Solution:1

make url manually

url: "/countries/tariff/"+escape($(this).val())  


Solution:2

You need to append that to the url parameters and leave out data, i.e.:

url: "/countries/tariff/" + $(this).val(),  


Solution:3

Pass the parameter directly in the url field instead of using data

$(document).ready(function(){         $('#CountriesIndexForm select').change(function(){                                  $.ajax({                            type: "GET",                               url: "/countries/tariff/" + escape($(this).val()),                               success: function(html){                                            $(this).parent().next('div').html(html);                               }                });          });   });  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »