Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8466555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:22:32+00:00 2026-06-10T15:22:32+00:00

I am using cakephp and want to use the jQuery.ajax to post to my

  • 0

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);
        }
    });
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T15:22:33+00:00Added an answer on June 10, 2026 at 3:22 pm

    Create a file lets suppose ‘site_constants.php’ containing some constant variables in app/Config folder. Define the following constants into it:

    <?php    
    define('HTTP_HOST', "http://" . $_SERVER['HTTP_HOST'].'/');
    if(HTTP_HOST == 'localhost' || HTTP_HOST == '127.0.0.1')
    {
         define('SITE_URL', HTTP_HOST.'app_folder_name/');
    }
    else
    {
         define('SITE_URL', HTTP_HOST);
    }
    

    Include it in app/Config/bootstrap.php

    require_once('site_constants.php');
    

    Define JS variable constant into your main layout file:

    <script type="text/javascript">
    var SITE_URL = '<?php echo SITE_URL?>';
    </script>
    

    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.

     $.ajax({
        url: SITE_URL+'controller/action',
        type: 'post',
        data: $('#form_data').serialize(),
        dataType: 'json',
        success: function(json) {
            console.log(json);
        }
    });
    

    This might looks long process to handle this task, but once done, it will help you in many senses.

    Here are the advantages:

    1. you can easily define some more constants (like any file path etc).
    2. you can put all your ajax code into external JS files.
    3. you can directly deploy it onto any server without changing in constants as well as work perfectly onto your localhost.
    4. following standard conventions etc.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using cakephp to build a survey and want to use javascript (specifically jquery
I'm using cakephp 2.1 just fine; I want to use jquery to have my
I am using ajax with jQuery in my cakePHP application. and my javascript function
I want to have cakephp paginate data and still use containable , but for
I want to use the autocomplete component for cakephp but instead of just autocompleting
I am using CakePHP and want to create a URL to a controller/view without
I am working on a CMS using CakePHP and I want to create a
Im using cakephp 2.0 and i have data submitted that i want to cleanup,
I am using CakePHP 2.0 and I want to load a concrete CSS style
I am using cakephp framework to build my application. I want that, whenever a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.