I am using cakephp and want to use the jQuery.ajax to post to my server. The issue is jQuery’s $.ajax appends the protocol and hostname, but also includes the current controller, so I cannot change the controller I am pasting too.
Is there a way to change the url: parameter in $.ajax? Here is my sample post code:
$.ajax({
url: 'controller/action',
type: 'post',
data: $('#form_data').serialize(),
dataType: 'json',
success: function(json) {
console.log(json);
}
});
Create a file lets suppose ‘site_constants.php’ containing some constant variables in app/Config folder. Define the following constants into it:
Include it in
app/Config/bootstrap.phpDefine JS variable constant into your main layout file:
Now you can use it anywhere into your website. And this is also a dynamic, you don’t need to change the url in every ajax method.
This might looks long process to handle this task, but once done, it will help you in many senses.
Here are the advantages: