I have a sample url website: http://mysite.com/
var host = window.location.protocol+"//"+window.location.hostname;
$.ajax({
type:"POST",
data: params,
url : host+'/forms/get_data.php',
success:function(data){
...othercodeblahblah
}
});
Why is it that when I try to check my firebug it makes the URL weird.
This is the sample output of firebug:
http://mysite.com/mysite.com/forms/get_data.php
With this url it now gives me:
"NetworkError: 404 Not Found - http://mysite.com/mysite.com/forms/get_data.php"
Shouldn’t it output like http://mysite.com/forms/get_data.php ?
Why is it giving me a wrong url path?
Your help would be greatly appreciated and rewarded!
Thank!
The reason is
window.location.protocolalready includes a colon (:).The
hostvariable therefor containshttp:://mysite.comjQuery picks up that you didn’t pass a full valid URL, so it prepends your hostname automatically.
The fix is changing
to
Edit
I created a jsfiddle with your code: http://jsfiddle.net/xH5ZV/
and the fixed code: http://jsfiddle.net/xH5ZV/1/
Notice in the fixed code you don’t get the hostname twice.