views:

32

answers:

2

Hey Guys,

Just a quick question about JQuery's Ajax function, more specifically, the url parameter.

My directory structure is like so:

contact.html
process.php

- elements
   - js
      - jquery.js
      - library.js

Code below

...

// Post with Ajax
$.ajax
({
    type : 'POST',
    url : '../../process.php',
    data : fulldata,
success: function()
{
    $('#contact_form #placeholder').html("<div id='success_box'><h3>Message Recieved</h3><p>We will be in touch very soon!</p></div>");
    $('#contact_form form').hide();
},
error: function(xhr)
{
    alert('Request Status: ' + xhr.status + ' Status Text: ' + xhr.statusText + ' ' + xhr.responseText);
}
});
return false;

Every-time I submit the form, it spits out an error, that I think is the url. How do I correctly specify the process.php file for submission with Ajax?

Thanks

+2  A: 

Your script will be running in a page at the same level as the process.php page. it does not run in the context of the js folder. therefore you can point it to the root of your application

"/process.php" or "process.php"

skyfoot
+2  A: 

The url you are using is relative. It will not be relative to the JS location, but rather to the page you are executing it on. So say you have this structure:

target.php page1.php js --myscript.js other --page2.php

If my JS had the URL of "target.php" it would work on page1.php, but NOT page2.php since it is not in the same directory of page2.

If you are not using some fancy Virtual hosts or something, a.k.a you are operating at the root of a domain (e.g. www.example.com/page1.php) you can make your URL relative to the root.

For example in your case:

URL: "/process.php"

The first slash will mean use the root of the website as a starting place no matter what page you are on.

Also, get firebug, it can tell you whether or not the URL is what is causing the problem: go to the NET tab and look at your XHRs. If it is getting a 404 not found, the URL is causing the problem and if your are getting a script error, it is definitely not the URL

Bob Fincheimer