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?

  • 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
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.

  • 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
-1

you can get your base URL as

var baseUrl = window.location.origin;

and then use an ajax call as

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.