Marco
Marco

Reputation: 2737

JQuery ajax url path (relative or absolute to what?)

I'm trying to run a php script using jquery's ajax. I don't understand how jquery defines the path for the script. I know how to make it work, but i need to understand why?

The php script is here

http://localhost/mywebsite/videography/include/data/data_videography_date.php

// filesystem

D:\public_html\mywebsite\videography\include\data\data_videography_date.php

the jquery script is here:

http://localhost/mywebsite/common/js/global.js

// filsesystem

D:\public_html\mywebsite\common\js\global.js

and im calling it like this:

$.ajax({
    type: "POST",
    url: "videography/include/data/data_videography_date.php"
}).done(function(data) {
    // done
}).fail(function(data) {
    // fail
}).always(function() {
    // always
});

I notice that jquery append http://localhost/mywebsite/ to the url. But how does jquery figures out the path?

Is it relative from the php script, from the jquery script, from WHERE exactly? Even does it works, i need to know why.

Upvotes: 2

Views: 14340

Answers (2)

Pablo
Pablo

Reputation: 6068

Per the additional information provided in the comments, the reason why http://localhost/mywebsite/ is added to the ajax URL is because the following base tag is defined in the HTML document

<base href="http://localhost/mywebsite/videography">

In this case all relative URLs including hyperlinks will be relative to http://localhost/mywebsite/ hence why the relative URL videography/include/data/data_videography_date.php results in http://localhost/mywebsite/videography/include/data/data_videography_date.php.

HTML base tag reference

Upvotes: 1

Sebastian Scholl
Sebastian Scholl

Reputation: 1105

Ajax is using localhost because your app is running on a local server.

Ajax will get the request origin from window.location.origin if given URL does not include an origin.

Upvotes: 5

Related Questions