absolute path is not working in ajax call - php

My file structure is something like this :
site
module
index.php
js
auto.js
ajax
auto.php
index.php
I have included js/auto.js in module/index.php,where auto.js is an ajax call which passes data to ajax/auto.php.
Edit site/index.php is also using js/auto.js and ajax call is working fine for site/index.php
my ajax call in auto.js is like :
$.ajax({
url : 'ajax/auto.php'
})
After page load in its giving error in console can not find site/module/ajax/auto.php.
I know its relative path error,what will be the correct relative path for this problem

Use slash at the beginning like this
$.ajax({
url: '/ajax/auto.php',
...
});

call it like
url : '../ajax/auto.php'
and it should work fine

Now its working Fine
$.ajax({
url : '/site/ajax/auto.php'
})

Related

How to call Ajax in wordpress in custom.php page?

I have a little weird problem
I want to call wordpress ajax url in a custom page.php in public_html
The weird thing is that I have two type of these pages which have two different path :
First one is : the_permalink()/page1
Second one is : the_permalink()/page1/page2
The problem is that the ajax url call is working fine in page1 path : the_permalink()/page1
But same functions with same way of calling is not working in page2 path : the_permalink()/page1/page2
Is there any suggestions ?
Note : i have tested the the SCRIPT code which call the ajax with alert() function and found that that the code stops when it becomes to ajax part is this code :
<script>
$(document).ready(function(){
$('.checknow').click(function(e){
e.preventDefault();
var data = "test";
//alert(ajaxurl);
$.ajax({
type:"POST",
url:ajaxurl,
data: {
action:'action_function_php',
data:data,
},
success:function(data){
$('.security-check-result').html(data);
}
});
});
return false;
});
</script>
Note : i am using wildcard *, so the permalink return is subdomain if this would help
I have also tried to type the url directly but didn't work also
Here is an example page ( will remove it soon ) - just for make things
clear -
https://gameloop.bramj.store/windows
in this page if u tried to search something in the search bar you will find the ajax return works fine
but if you move to
https://gameloop.bramj.store/windows/download
you will notice that every single ajax code in the page is not working.
Thank you so much the problem is solved
i had to put the current main url ( domain/ subdomain ) without any slugs
so the best solution for this case is use this url :
var ajaxurl = "https://<?php echo $_SERVER['HTTP_HOST'];?>/wp-admin/admin-ajax.php";
You need to use wp_localize_script in order to have access to ajaxurl global.
If you don't want to do that then you can replace ajaxurl with <?php echo admin_url('admin-ajax.php');?> if you javascript is inside a PHP file.

ajax call to php function without changing url

I am working with Symfony2 and I am using Dropzone.js with the bundle OneUploader to upload several images at a time.
Dropzone.js has the option of deleting an image that has been selected to upload, but the problem is that when I click on 'Remove' the image is only removed on the client side, not on the server. So what I need is to make an ajax call in mi dropzone.js to a function in my php that deletes the file from the server. The question is: how can I make that call in order that the url is not changed? I am using this one but it is not working, because the url is changing:
$.ajax({
url: 'delete.php',
method:'POST',
data: {name: file.name}
});
Thanks in advance.
Finally I was doing something wrong... I was making the ajax call in a .js file, so I could not use the path form in the url in the ajax call (twig language). So I have solved it making the ajax call in a .html.twig file and using a path in the url, that calls a routing, and it calls an action in a Controller that makes the deletion.
By the way, thanks for your help!

call a php file who write a cookie, the call is done using ajax

I'm trying to make a call to a php script who write cookie
and I make the call with jquery (ajax)
here is all my codes:
this is the code who can anyone use to make the call:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="http://www.mywebsite.com/generated.js"></script>
<div id="generated"></div>
for the file "generated.js", I make the ajax call:
$(document).ready(function(){
$.ajax({
url : "generated.php",
type : "POST",
data : "",
success : function(n){
$( "#generated" ).html( n ); }
});
});
and here is the "generated.php" where i create the cookie:
<?php setcookie("TestCookie", "test"); ?>
when i try to use the first code in a html file in the same directory as the other it's working, but when i try to use it in another server it doesn't work, and thanks to all of you
It's a cross domaine restriction i think, you can't call a page outside your domaine via ajax.
a work around is to use CURL.
So your architecture should look's like :
A JS who call a local php file.
Your php file will call via CURL your remote file and get response.
In your ajax done function, you can now have your page result.
Wish it can helps.

AJAX, SEO and PHP folder trouble

I'm creating an AJAX script that reads a specific file, which name is identified with a GET var. The beginning of the script in index.php is:
var expFile = '<?php echo $_GET['text_name']; ?>';
$(document).ready(function () {
$.ajax({
url: expFile+'.xml',
type: 'get',
dataType: 'xml',
success: function (data, textStatus) {
// Parses the content, which is escaped into a "text" tag in the xml, and puts it
//into an html div with a "content" class"
var content = decodeURIComponent($(data).find('text').text());
$('.content').html(content);
}
});
});
And everything went ok. The file is read and the thing is shown correctly.
The XML file, that is in the same folder of the index.php file, is read directly from AJAX.
Now i'm using a mod_rewrite in order to make the URL SEO-Friendly.
When i type the dirty URL (http://www.mysite.com/index.php?text_name=name-of-the-file-to-read) it's OK.
But when i type the rewritten url (which is http://www.mysite.com/lyrics/name-of-the-file-to-read) the content is not shown.
I know that AJAX is client-side, while mod_rewrite is server-side, but I don't know how to reach a parent folder (that really doesn't exist) from the "url" parameter of the $.ajax or an absolute link like url: 'http://...' (but it goes against Same Origin Policy).
Help me please!!!
Francesco's answer, initially put into the question:
SOLVED.
THE PROBLEM was with the script tag. I had to put a ../ to the jquery href
<script type="text/javascript" src="../libraries/jquery.min.js"></script>
Then I added ../ to the URL and everything went fine.
The URL is having .xml appended by the function itself - on this line:
url: expFile+'.xml',
Is that still there when you are trying with the SEO friendly URL? If so, I'd remove that appended .xml and try again.

CodeIgniter, PHP, jQuery and AJAX

I am wondering about how people go about using CodeIgniter and jQuery for AJAX.
In your AJAX request you have
{
url : ???,
data : {a:1,b:2},
success: ....
}
So, how do you build the URL?
Do you
have all your JavaScript in your view files, and just use site_url() to build the URL
have all your JavaScript in external js files, have a header view you include that has something like <script>var base_url = '<?php echo site_url(); ?>';</script>. Then in your external js files have url: base_url+'rest/of/path/';
some other method?
I have my all my js in an external file and load it in my template.
For specific ajax requests, just call the page as you normally would.
$.ajax({
type: 'POST',
url: '/ajax/login',
data: blabla,
success: function(data) {
// do something
},
dataType: 'json');
});
In answer to your question, I've had no need to specify the base url, as putting '/' before the controller name sets the root of the site automatically. You could also use ../ etc
if you are writing your ajax in external file then you can define your base url in the view file like
<script>
var base_url = '<?php echo base_url(); ?>';
</script>
Then on your external ajax file write
url : base_url+'controllerName/functionName',
It can also be done by loading a view that contains your JavaScript.
I currently load JavaScript from a view at the end of the rendered page. Since it is a PHP file with html <script> in it, you can use the URL helper functions like site_url() to generate the URLs you need for each function.
An example view might contain:
<script>
$.ajax{
url : "<?=site_url("controller/function")?>",
data : {a:1,b:2},
}
</script>
That will get you CodeIgniter generated URLs for your JavaScript. You could even pass variables into the view for more control over your js.
Usually I make a small javascript in my header, in which I create a base_url and site_url variable (usually being properties of an object I name CI, but that's a personal preeference). I fill these values by echoing the values with PHP. If you make that the first loaded script, you'll always have the site_url available in JS.
Since I'm on mobile I can't post the source now.

Categories