Jquery Ajax url path Issue

74,495

Solution 1

If the files you're trying to contact are in the root you can use /[file].php so that no matter which page you're on the path will be correct. It sounds like you have a relative path issue. Do you get any errors in the console?

Solution 2

To understand ajax url, I have two things that should always be remembered.

1. If we put slash at the beginning of ajax url, ajax url pattern will be `hostname/yourFile` example:

// current url: http://sample.com/users
// ajax code load from users page

$.ajax({
   url: '/yourFile.php',
   ...
});

// ajax url will be: http://sample.com/yourFile.php

2. If we don't use slash at the beginning, ajax url will add to the current url in the browser. example:

// current url: http://sample.com/users
// ajax code load from users page

$.ajax({
    url: 'yourFile.php',
    ...
});

//...ajax url will be http://simple.com/users/yourFile.php

I hope this things can help people who want to use ajax. Happy coding, thanks.

Share:
74,495
user2372160
Author by

user2372160

Updated on July 21, 2022

Comments

  • user2372160
    user2372160 almost 2 years

    I am trying to design a message system for my website, but i can't get my ajax running. So I make a simpler version of the interaction between the files.

    Here is my file both test.php and load.php is under the root folder ( public_html).

    i have the ajax function in test.php. load.php is just simply echoing "wow".

    $("#test").click(function(){
     alert("Clicked.");
    
     $.ajax({
         url:"/load.php",
         type:"POST",
         beforeSend: function(){
             alert("testing");   
         },
         success:function(result){
            $("#result").html(result);
            alert("  sss  "+result);
         }
     }).error(function(){alert("wrong");});
     });
    

    Now It works perfectly.

    ...........how to set relative path ...................

    Here is the more complicated design

    3 files, all in different folder :

    1. messages.php (under root)
      • showing all the messages
    2. control.php (root/panels)
      • a panel will be included in messages.php
    3. load.php (root/functions)
      • control.php will use ajax to call it then display result in control.php

    so when user load in messages.php, it will load control.php then run the ajax call control.php.

    I am so confused about how to set up these paths for ajax
    ( including control.php in messages.php works fine)

    Thanks

  • user2372160
    user2372160 almost 10 years
    ya it worked. but how do i set the relative path if 3 files are in different folder ?
  • Alex
    Alex almost 10 years
    That's when you'd have to use an absolute path, or create a dynamic relative path based on the current path. But absolute is the easier option.
  • user2372160
    user2372160 almost 10 years
    like go all the way back to root(public_html) then use "../folder/folder/file.php" ?
  • Alex
    Alex almost 10 years
    Yeah, but ../ means 'the parent folder'. You'd want /folder/folder/file.php to get directly to a specific file.
  • user2372160
    user2372160 almost 10 years
    k i see now. its not like require and include in php. Ajax url is using the absolute path. I got confused because i got many snippet php files including in my main pages and they also include some other files from different folder. the path setting is a mess. :( thank you!
  • MonsterMMORPG
    MonsterMMORPG about 8 years
    ty let me try this :d
  • Aaron Liu
    Aaron Liu almost 8 years
    How do I check if a file exists in another folder (different from the folder where AJAX code is located)?