Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
180 views
in Technique[技术] by (71.8m points)

javascript - Jquery Ajax url path Issue

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...