AJAX调用源自哪个目录?

If I have the below code in an external JavaScript file; if the PHP file I'm calling is located in the same directory, do I have to add "../directory/filename.php"? The php script is not running, and I've posted the script in full in another thread, but I think It may be related to the "URL" in my $.ajax call.

$.ajax({
    type: 'POST',
    url: '../script/mail.php',
    data: {fname:firstName, lname:lastName, email:emailVal, comment:comments, fileName:fileName},
    success: function(){
        $('form').each(function() {
            this.reset();
        });
    }
});

Previous Question: PHP Mailer script not completing after ajax call and form submit

URLs in JavaScript programs (that are embedded in a webpage) are relative to the URL of the page.

if the PHP file I'm calling is located in the same directory, do I have to add "../directory/filename.php"?

If filename.php is in the same directory as the HTML document, then no, you can just do filename.php.

If filename.php is in the same directory as the external JS file, then … maybe, but you need to look at where the HTML document is, not the JS file.

Same composition as link's href

For me, I always use relative path from web directory.

../ will take you back a directory

./ gives you the relative directory

~/ gives you your /home/user directory

/ gives you your root directory

the directory is relative to the file that is calling it not the .js file if it is is in another directory.

and if you are unsure you can always test by first giving it the full url http://<file location>

I usually code my no-js fallback first. So you have the link that is triggering your function being called first and you know that it goes to the right place:

<a href="script/mail.php" id="send-mail" />

Then you can just scrape the url straight out of the link:

//add click listener
$('#send-mail').click(function(e) {
  e.preventDefault();

  //get your link
  var myUrl = $(this).attr('href');

  //do something ajaxy
  $.ajax({
    type: 'POST',
    url: myUrl,
    data: {somedata: data},
    success: function(){
      //some success actions
    }
  });
}

For plus points you could use some php to generate an absolute url to use in your link.