Javascript get real path in any situation ($.ajax) - javascript

My project has the following structure:
Sil
-> css
-> js
-> clients
-> index.php
-> response.php
index.php
My problem is on the folder clients.
My url can have multiple choices, some of them are:
http://localhost/Sil/clients/
http://localhost/Sil/clients/all/
http://localhost/Sil/clients/stackoverflow/2
http://localhost/Sil/clients/stackoverflow/2/technology/5
As you can see I use .htaccess to write rules.
That said, the problem here is with the url inside the $.ajax.. I use the following code inside the clients/index.php file.
$.ajax({
url : 'response.php?type=add',
});
This code works perfectly if and only if my url is http://localhost/Sil/clients/.
In case the url are the other options said above, it won't work, because the file URL output will be:
http://localhost/Sil/clients/all/response.php?type=add
Which is clearly wrong.
I have already solved this issue by using a complete url, but I do not believe this is the best approach since when I have to upload the files to the server by FTP I have to change in every $.ajax from localhost to my domain name.
$.ajax({
url : 'http://localhost/Sil/response.php?type=add',
});
What other options do I have? I've tried, without success, the window.location.pathname.

You can either start the URL with a slash in order to specify the "root directory":
$.ajax({
url : '/Sil/clients/response.php?type=add',
});
or use a <base> tag in HTML:
<base href="http://localhost/Sil/clients/" />
<script>
$.ajax({
url : 'response.php?type=add',
});
</script>

Related

How to write path to files in Magento? File is not found, even when I set the absolute path

everyone!
I try to create custom application sending (to emails) form the site on Magento.
For doing it, I call post.php file in this way:
$('form#send-profile').submit(function(event) {
event.preventDefault();
var output = true,
array = $(this).serialize();
if (output) {
$.post('post.php', array, function(data) {
$('input[type="text"], textarea').val('');
});
};
});
I`ve placed post.php into the root folder (and tried it with different folders too).
But I had this result in any conditions:
POST http://vescorte.com/post.php 404 (Not Found)
Maybe Magento has some special way to set paths? Tell me please, if you know how to cope with this problem.
First of all , which version of magento do you use ?
Next, if you want to development any new function , you need create a module
try this link magento-2-module-development
In the template file in which you mentioned JS code, you can mention the Magento base URL function to get full base URL like below:
$.post('<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB); ?>post.php', array, function(data) {
$('input[type="text"], textarea').val('');
});
Still if you face issue then try to remove all the code from post.php and run with simple text to check file request complete or not.

how to change url for javascript in opencart

i have been working on the opencart frontend. And i want to make a frontend structure such that products uploaded by a particular vendor is shown. For that i use the url 'user/vendor_name'. And i have made changes in htaccess file for such url and after that i have changed the link function in url.php file for such cases. So now if a user clicks anywhere in the website the url will show 'user/vendor_name/index.php......'. But the url in javascript doesnt use $this->link function and those changes of url.php file doesnot take effect and thus it redirects to the original url.
Please help me out on this.
You would have to modify the controllers for each template containing such URLs and make sure You are setting a vendor name to a PHP variable accessible by the template:
$this->data['vendor'] = $vendor_information['name'];
supposing the vendor name is stored in a variable $vendor_name under the index name. This is only an example. Now in each template identify such URL with the JS part:
$.ajax({
url: 'index.php?route=checkout/cart/add' // + ...
// ...
});
and change it to:
$.ajax({
url: 'vendor/<?php echo $vendor; ?>/index.php?route=checkout/cart/add' // + ...
// ...
});
This should solve your problem.

How to use `GET` parameters in a HTML document?

I have a HTML document and I want to send a parameter.
Let's name the document Categoty, and all it do it sends a request to the server (via Ajax) to get something on this categoty.
Suppose I'm using PHP, I would simply send the parameter:
localhost/Categoty.php?cat=somecat,
and read it in the PHP document: $_GET['cat'].
My question is if there's something similar in HTML (or Javascript)?
I though about hashtag (.i.e. categoty.html#somecat) , but I'm using jQuery mobile in my Android Hybrid application so it makes some issues.
Thanks in advance.
use the jQuery URL Parser in this link and then try:
jQuery.url().param('cat');
you can also access all other info we usually need in terms of dealing with GET parameters, like:
jQuery.url().attr('protocol');
returns the URL protocol like: http or https
jQuery.url().attr('host')
returns the host like stackoverflow.com
you can also get the segments of your URL like this:
jQuery.url().segment(1);//for instance in this very page's url it returns "posts"
at the end:
jQuery.url().param('cat') in javascript does the same as $_GET['cat'] does in PHP
jQuery ajax can do it , use $.ajax();
$.ajax({
type: 'GET',
url: 'http://localhost/Categoty.php?cat=somecat',
success: function(responseTxt,statusTxt,xhr){
}
});

How to use a relative path for (jquery) ajax url

I have an ajax function:
$.post("/ajax.php",{query:data},function(result){...
the location of ajax.php is in the root of my domain (i.e. site.com/ajax.php)
this same script gets called when the user is in a folder (i.e. site.com/m/)
The problem I face is that I want to reference the root directory. Usually /file.php refers to the home directory as opposed to file.php.
My requests are being sent to http://site.com/m/ajax.php.
Does anyone know what I should do?
I ended up doing as #Ohgodwhy suggested:
$.post(
"<?php echo $_SERVER['HTTP_HOST'];?>`/ajax.php",
{query:data},
function(result){
// alert(result);
});
So far, it's working for me!

how not to hard-code codeigniter link in js file

i wrote the routine below to retrieve city based on selected country for my codeigniter application.
$(document).ready(function() {
$("select#cbo_country").change(function() {
$.post("http://localhost/main/index.php/city/get_data_by_country", {
int_country_id : $(this).val()
},
function(data) {
// some code here
},'json');
})
});
as you can see, i hard-coded the url (http://localhost/main/index.php/city/get_data_by_country) and i know it's a bad practice but i can't help it.
is there a nice clean way to not hard-code the url? i used to use codeigniter's base_url(), but since i move the routine to a js file, i am no longer able to use the function.
Taken (mostly) from my answer on How to get relative path in Javascript?.
You've got two options:
Build a configuration/ preferences object in JavaScript which contains all your environment specific settings:
var config = {
base: "<?php echo base_url(); ?>",
someOtherPref: 4
};
and then prefix the AJAX url with config.base.
You have to place the config object in a place which is parsed by PHP; the standard choice is inside the <head> HTML element. Don't worry its a small config object, whose contents can change on each page, so it's perfectly warrented to stick it in there.
Use the <base /> HTML tag to set the URL prefix for all relative URL's. This affects all relative URL's: image's, links etc.
Personally, I'd go for option 1. You'll most likely find that config object coming in handy elsewhere.
Change "http://localhost/main/index.php/city/get_data_by_country" to "/main/index.php/city/get_data_by_country" and it will work no matter what your base url is.
This works because the / before main/index.php says "start at the root and go the the index.php file in the main folder".
This is unless your document root folder is set to the main folder, if so, take out the main
One thing you can do is add a data-baseurl attribute to an element on the page (the body element works). Then you can grab that from the JavaScript file.
<body data-baseurl="<?=base_url()?>">
Then in JavaScript:
$("select#cbo_country").change(function() {
var baseURL = $('body').data('baseurl')
$.post(baseURL+"city/get_data_by_country", {
int_country_id : $(this).val()
},
function(data) {
// some code here
},'json');
})

Categories

Resources