Setting AJAX URL for custom module












6















I have a JavaScript file in /js/mds/mds_collivery.js. In it I have an AJAX call that should go to magento_install_url/collivery/ajax/cptypes. It will only be used in /checkout/onepage/.



However, if I set the URL to be:



url : "/collivery/ajax/cptypes",


it tries to access



http://localhost/collivery/ajax/cptypes


and if I set the URL to:



url : "collivery/ajax/cptypes",


it tries to access



http://localhost/magento/index.php/checkout/onepage/collivery/ajax/cptypes


I currently hard coded it to



http://localhost/magento/index.php/collivery/ajax/cptypes


but this isn't ideal as the setup procedure for my module would require users to edit this file before it can work.



Is there a JS variable or another way to get the Magento Install Location inside JavaScript?










share|improve this question




















  • 1





    I don't believe it is possible to get a Magento Base URL into a .js file. If its only going to be used in one area, the following should work: url : "../../collivery/ajax/cptypes", One thought. When working with Magento locally, you should always make it as a subdomain (like www.magento.localhost), else you will run into cookie issues. In fact, Magento won't install without it being in a subdomain. If you setup Magento as a subdomain, your first answer, url : "/collivery/ajax/cptypes", would work.

    – kab8609
    May 8 '13 at 20:53











  • Thank you for your response! I don't think I have enough rep to upvote your answer yet. So sorry about that. I'm assuming the best solution is then to use ../../ instead of just /? This is already a huge step forward in the right direction, can't believe I didn't think of this! So thanks. :)

    – Xethron
    May 8 '13 at 21:02








  • 1





    @kab8609 The ../../ works perfectly! Going to use that for the time being.

    – Xethron
    May 8 '13 at 21:12
















6















I have a JavaScript file in /js/mds/mds_collivery.js. In it I have an AJAX call that should go to magento_install_url/collivery/ajax/cptypes. It will only be used in /checkout/onepage/.



However, if I set the URL to be:



url : "/collivery/ajax/cptypes",


it tries to access



http://localhost/collivery/ajax/cptypes


and if I set the URL to:



url : "collivery/ajax/cptypes",


it tries to access



http://localhost/magento/index.php/checkout/onepage/collivery/ajax/cptypes


I currently hard coded it to



http://localhost/magento/index.php/collivery/ajax/cptypes


but this isn't ideal as the setup procedure for my module would require users to edit this file before it can work.



Is there a JS variable or another way to get the Magento Install Location inside JavaScript?










share|improve this question




















  • 1





    I don't believe it is possible to get a Magento Base URL into a .js file. If its only going to be used in one area, the following should work: url : "../../collivery/ajax/cptypes", One thought. When working with Magento locally, you should always make it as a subdomain (like www.magento.localhost), else you will run into cookie issues. In fact, Magento won't install without it being in a subdomain. If you setup Magento as a subdomain, your first answer, url : "/collivery/ajax/cptypes", would work.

    – kab8609
    May 8 '13 at 20:53











  • Thank you for your response! I don't think I have enough rep to upvote your answer yet. So sorry about that. I'm assuming the best solution is then to use ../../ instead of just /? This is already a huge step forward in the right direction, can't believe I didn't think of this! So thanks. :)

    – Xethron
    May 8 '13 at 21:02








  • 1





    @kab8609 The ../../ works perfectly! Going to use that for the time being.

    – Xethron
    May 8 '13 at 21:12














6












6








6








I have a JavaScript file in /js/mds/mds_collivery.js. In it I have an AJAX call that should go to magento_install_url/collivery/ajax/cptypes. It will only be used in /checkout/onepage/.



However, if I set the URL to be:



url : "/collivery/ajax/cptypes",


it tries to access



http://localhost/collivery/ajax/cptypes


and if I set the URL to:



url : "collivery/ajax/cptypes",


it tries to access



http://localhost/magento/index.php/checkout/onepage/collivery/ajax/cptypes


I currently hard coded it to



http://localhost/magento/index.php/collivery/ajax/cptypes


but this isn't ideal as the setup procedure for my module would require users to edit this file before it can work.



Is there a JS variable or another way to get the Magento Install Location inside JavaScript?










share|improve this question
















I have a JavaScript file in /js/mds/mds_collivery.js. In it I have an AJAX call that should go to magento_install_url/collivery/ajax/cptypes. It will only be used in /checkout/onepage/.



However, if I set the URL to be:



url : "/collivery/ajax/cptypes",


it tries to access



http://localhost/collivery/ajax/cptypes


and if I set the URL to:



url : "collivery/ajax/cptypes",


it tries to access



http://localhost/magento/index.php/checkout/onepage/collivery/ajax/cptypes


I currently hard coded it to



http://localhost/magento/index.php/collivery/ajax/cptypes


but this isn't ideal as the setup procedure for my module would require users to edit this file before it can work.



Is there a JS variable or another way to get the Magento Install Location inside JavaScript?







module javascript url ajax






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 28 '17 at 17:09









Prince Patel

13.3k54676




13.3k54676










asked May 8 '13 at 20:34









XethronXethron

14626




14626








  • 1





    I don't believe it is possible to get a Magento Base URL into a .js file. If its only going to be used in one area, the following should work: url : "../../collivery/ajax/cptypes", One thought. When working with Magento locally, you should always make it as a subdomain (like www.magento.localhost), else you will run into cookie issues. In fact, Magento won't install without it being in a subdomain. If you setup Magento as a subdomain, your first answer, url : "/collivery/ajax/cptypes", would work.

    – kab8609
    May 8 '13 at 20:53











  • Thank you for your response! I don't think I have enough rep to upvote your answer yet. So sorry about that. I'm assuming the best solution is then to use ../../ instead of just /? This is already a huge step forward in the right direction, can't believe I didn't think of this! So thanks. :)

    – Xethron
    May 8 '13 at 21:02








  • 1





    @kab8609 The ../../ works perfectly! Going to use that for the time being.

    – Xethron
    May 8 '13 at 21:12














  • 1





    I don't believe it is possible to get a Magento Base URL into a .js file. If its only going to be used in one area, the following should work: url : "../../collivery/ajax/cptypes", One thought. When working with Magento locally, you should always make it as a subdomain (like www.magento.localhost), else you will run into cookie issues. In fact, Magento won't install without it being in a subdomain. If you setup Magento as a subdomain, your first answer, url : "/collivery/ajax/cptypes", would work.

    – kab8609
    May 8 '13 at 20:53











  • Thank you for your response! I don't think I have enough rep to upvote your answer yet. So sorry about that. I'm assuming the best solution is then to use ../../ instead of just /? This is already a huge step forward in the right direction, can't believe I didn't think of this! So thanks. :)

    – Xethron
    May 8 '13 at 21:02








  • 1





    @kab8609 The ../../ works perfectly! Going to use that for the time being.

    – Xethron
    May 8 '13 at 21:12








1




1





I don't believe it is possible to get a Magento Base URL into a .js file. If its only going to be used in one area, the following should work: url : "../../collivery/ajax/cptypes", One thought. When working with Magento locally, you should always make it as a subdomain (like www.magento.localhost), else you will run into cookie issues. In fact, Magento won't install without it being in a subdomain. If you setup Magento as a subdomain, your first answer, url : "/collivery/ajax/cptypes", would work.

– kab8609
May 8 '13 at 20:53





I don't believe it is possible to get a Magento Base URL into a .js file. If its only going to be used in one area, the following should work: url : "../../collivery/ajax/cptypes", One thought. When working with Magento locally, you should always make it as a subdomain (like www.magento.localhost), else you will run into cookie issues. In fact, Magento won't install without it being in a subdomain. If you setup Magento as a subdomain, your first answer, url : "/collivery/ajax/cptypes", would work.

– kab8609
May 8 '13 at 20:53













Thank you for your response! I don't think I have enough rep to upvote your answer yet. So sorry about that. I'm assuming the best solution is then to use ../../ instead of just /? This is already a huge step forward in the right direction, can't believe I didn't think of this! So thanks. :)

– Xethron
May 8 '13 at 21:02







Thank you for your response! I don't think I have enough rep to upvote your answer yet. So sorry about that. I'm assuming the best solution is then to use ../../ instead of just /? This is already a huge step forward in the right direction, can't believe I didn't think of this! So thanks. :)

– Xethron
May 8 '13 at 21:02






1




1





@kab8609 The ../../ works perfectly! Going to use that for the time being.

– Xethron
May 8 '13 at 21:12





@kab8609 The ../../ works perfectly! Going to use that for the time being.

– Xethron
May 8 '13 at 21:12










2 Answers
2






active

oldest

votes


















11














in app/design/frontend/{interface}/{theme}/template/checkout/onepage.phtml add this code somewhere at the top of the template:



<script type="text/javascript">
var baseUrl = '<?php echo Mage::getUrl('');?>';
</script>


now you should be able to use in your js file this:



url : baseUrl + "/collivery/ajax/cptypes",


Actually you can add the script anywhere in the template (header, footer, ...) just make sure it's in the page you need. Since the checkout page is the only one you need, I suggested adding it in the onepage template.






share|improve this answer
























  • Awesome! But, how do plugins usually do this? I mean, there has to be a "right" way of doing this?

    – Xethron
    May 8 '13 at 21:06











  • Oh, and I seem to require 15 rep to upvote an answer. If you and @kab8609 upvotes my question, I could upvote your answers as I do believe in giving credit where credit is due. :)

    – Xethron
    May 8 '13 at 21:08






  • 1





    I learn something new everyday. Good catch!

    – kab8609
    May 8 '13 at 21:11






  • 2





    The right way if doing this I think it is to pass the base url (or full URL) to your js function that does the ajax request.

    – Marius
    May 8 '13 at 21:31






  • 3





    Why not putting the full url into the getUrl call? Then if you change the frontname, the link is still correct. Mage::getUrl('collivery/ajax/cptype')

    – Fabian Blechschmidt
    May 9 '13 at 6:45





















0














you can get your base url as



var baseUrl = window.location.origin;



and then use in ajax call as



url : baseUrl + "/collivery/ajax/cptypes",





share








New contributor




user76257 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "479"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f3565%2fsetting-ajax-url-for-custom-module%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    11














    in app/design/frontend/{interface}/{theme}/template/checkout/onepage.phtml add this code somewhere at the top of the template:



    <script type="text/javascript">
    var baseUrl = '<?php echo Mage::getUrl('');?>';
    </script>


    now you should be able to use in your js file this:



    url : baseUrl + "/collivery/ajax/cptypes",


    Actually you can add the script anywhere in the template (header, footer, ...) just make sure it's in the page you need. Since the checkout page is the only one you need, I suggested adding it in the onepage template.






    share|improve this answer
























    • Awesome! But, how do plugins usually do this? I mean, there has to be a "right" way of doing this?

      – Xethron
      May 8 '13 at 21:06











    • Oh, and I seem to require 15 rep to upvote an answer. If you and @kab8609 upvotes my question, I could upvote your answers as I do believe in giving credit where credit is due. :)

      – Xethron
      May 8 '13 at 21:08






    • 1





      I learn something new everyday. Good catch!

      – kab8609
      May 8 '13 at 21:11






    • 2





      The right way if doing this I think it is to pass the base url (or full URL) to your js function that does the ajax request.

      – Marius
      May 8 '13 at 21:31






    • 3





      Why not putting the full url into the getUrl call? Then if you change the frontname, the link is still correct. Mage::getUrl('collivery/ajax/cptype')

      – Fabian Blechschmidt
      May 9 '13 at 6:45


















    11














    in app/design/frontend/{interface}/{theme}/template/checkout/onepage.phtml add this code somewhere at the top of the template:



    <script type="text/javascript">
    var baseUrl = '<?php echo Mage::getUrl('');?>';
    </script>


    now you should be able to use in your js file this:



    url : baseUrl + "/collivery/ajax/cptypes",


    Actually you can add the script anywhere in the template (header, footer, ...) just make sure it's in the page you need. Since the checkout page is the only one you need, I suggested adding it in the onepage template.






    share|improve this answer
























    • Awesome! But, how do plugins usually do this? I mean, there has to be a "right" way of doing this?

      – Xethron
      May 8 '13 at 21:06











    • Oh, and I seem to require 15 rep to upvote an answer. If you and @kab8609 upvotes my question, I could upvote your answers as I do believe in giving credit where credit is due. :)

      – Xethron
      May 8 '13 at 21:08






    • 1





      I learn something new everyday. Good catch!

      – kab8609
      May 8 '13 at 21:11






    • 2





      The right way if doing this I think it is to pass the base url (or full URL) to your js function that does the ajax request.

      – Marius
      May 8 '13 at 21:31






    • 3





      Why not putting the full url into the getUrl call? Then if you change the frontname, the link is still correct. Mage::getUrl('collivery/ajax/cptype')

      – Fabian Blechschmidt
      May 9 '13 at 6:45
















    11












    11








    11







    in app/design/frontend/{interface}/{theme}/template/checkout/onepage.phtml add this code somewhere at the top of the template:



    <script type="text/javascript">
    var baseUrl = '<?php echo Mage::getUrl('');?>';
    </script>


    now you should be able to use in your js file this:



    url : baseUrl + "/collivery/ajax/cptypes",


    Actually you can add the script anywhere in the template (header, footer, ...) just make sure it's in the page you need. Since the checkout page is the only one you need, I suggested adding it in the onepage template.






    share|improve this answer













    in app/design/frontend/{interface}/{theme}/template/checkout/onepage.phtml add this code somewhere at the top of the template:



    <script type="text/javascript">
    var baseUrl = '<?php echo Mage::getUrl('');?>';
    </script>


    now you should be able to use in your js file this:



    url : baseUrl + "/collivery/ajax/cptypes",


    Actually you can add the script anywhere in the template (header, footer, ...) just make sure it's in the page you need. Since the checkout page is the only one you need, I suggested adding it in the onepage template.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered May 8 '13 at 21:04









    MariusMarius

    164k28312662




    164k28312662













    • Awesome! But, how do plugins usually do this? I mean, there has to be a "right" way of doing this?

      – Xethron
      May 8 '13 at 21:06











    • Oh, and I seem to require 15 rep to upvote an answer. If you and @kab8609 upvotes my question, I could upvote your answers as I do believe in giving credit where credit is due. :)

      – Xethron
      May 8 '13 at 21:08






    • 1





      I learn something new everyday. Good catch!

      – kab8609
      May 8 '13 at 21:11






    • 2





      The right way if doing this I think it is to pass the base url (or full URL) to your js function that does the ajax request.

      – Marius
      May 8 '13 at 21:31






    • 3





      Why not putting the full url into the getUrl call? Then if you change the frontname, the link is still correct. Mage::getUrl('collivery/ajax/cptype')

      – Fabian Blechschmidt
      May 9 '13 at 6:45





















    • Awesome! But, how do plugins usually do this? I mean, there has to be a "right" way of doing this?

      – Xethron
      May 8 '13 at 21:06











    • Oh, and I seem to require 15 rep to upvote an answer. If you and @kab8609 upvotes my question, I could upvote your answers as I do believe in giving credit where credit is due. :)

      – Xethron
      May 8 '13 at 21:08






    • 1





      I learn something new everyday. Good catch!

      – kab8609
      May 8 '13 at 21:11






    • 2





      The right way if doing this I think it is to pass the base url (or full URL) to your js function that does the ajax request.

      – Marius
      May 8 '13 at 21:31






    • 3





      Why not putting the full url into the getUrl call? Then if you change the frontname, the link is still correct. Mage::getUrl('collivery/ajax/cptype')

      – Fabian Blechschmidt
      May 9 '13 at 6:45



















    Awesome! But, how do plugins usually do this? I mean, there has to be a "right" way of doing this?

    – Xethron
    May 8 '13 at 21:06





    Awesome! But, how do plugins usually do this? I mean, there has to be a "right" way of doing this?

    – Xethron
    May 8 '13 at 21:06













    Oh, and I seem to require 15 rep to upvote an answer. If you and @kab8609 upvotes my question, I could upvote your answers as I do believe in giving credit where credit is due. :)

    – Xethron
    May 8 '13 at 21:08





    Oh, and I seem to require 15 rep to upvote an answer. If you and @kab8609 upvotes my question, I could upvote your answers as I do believe in giving credit where credit is due. :)

    – Xethron
    May 8 '13 at 21:08




    1




    1





    I learn something new everyday. Good catch!

    – kab8609
    May 8 '13 at 21:11





    I learn something new everyday. Good catch!

    – kab8609
    May 8 '13 at 21:11




    2




    2





    The right way if doing this I think it is to pass the base url (or full URL) to your js function that does the ajax request.

    – Marius
    May 8 '13 at 21:31





    The right way if doing this I think it is to pass the base url (or full URL) to your js function that does the ajax request.

    – Marius
    May 8 '13 at 21:31




    3




    3





    Why not putting the full url into the getUrl call? Then if you change the frontname, the link is still correct. Mage::getUrl('collivery/ajax/cptype')

    – Fabian Blechschmidt
    May 9 '13 at 6:45







    Why not putting the full url into the getUrl call? Then if you change the frontname, the link is still correct. Mage::getUrl('collivery/ajax/cptype')

    – Fabian Blechschmidt
    May 9 '13 at 6:45















    0














    you can get your base url as



    var baseUrl = window.location.origin;



    and then use in ajax call as



    url : baseUrl + "/collivery/ajax/cptypes",





    share








    New contributor




    user76257 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.

























      0














      you can get your base url as



      var baseUrl = window.location.origin;



      and then use in ajax call as



      url : baseUrl + "/collivery/ajax/cptypes",





      share








      New contributor




      user76257 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.























        0












        0








        0







        you can get your base url as



        var baseUrl = window.location.origin;



        and then use in ajax call as



        url : baseUrl + "/collivery/ajax/cptypes",





        share








        New contributor




        user76257 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.










        you can get your base url as



        var baseUrl = window.location.origin;



        and then use in ajax call as



        url : baseUrl + "/collivery/ajax/cptypes",






        share








        New contributor




        user76257 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.








        share


        share






        New contributor




        user76257 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        answered 7 mins ago









        user76257user76257

        1




        1




        New contributor




        user76257 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.





        New contributor





        user76257 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






        user76257 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Magento Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f3565%2fsetting-ajax-url-for-custom-module%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Polycentropodidae

            Magento 2 Error message: Invalid state change requested

            Paulmy