jQuery .ajax() url parameter not taking concatenated string?
I have a piece of javascript that may be executed from a variety of pages with different URLs. The code is working properly in this case (the key here is the url parameter, nothing else will be changing):
$.ajax({
type: "POST",
url: "./aja开发者_C百科x/getPageTitle",
data: "pageID="+$("#pageToEdit").val(),
success: function(pageName){
$("#updatedPageName").val(pageName);
},
error: function(req,error){
if(error === 'error'){error = req.statusText;}
alert("There was an error: " + error);
}
});
When I try to modify the url parameter to something like this:
$.ajax({
type: "POST",
url: BASE_URL+"ajax/getPageTitle",
data: "pageID="+$("#pageToEdit").val(),
success: function(pageName){
$("#updatedPageName").val(pageName);
},
error: function(req,error){
if(error === 'error'){error = req.statusText;}
alert("There was an error: " + error);
}
});
where BASE_URL is a constant, it causes an error. It seems the request isn't even being sent (no 404 errors in the console in firebug when I try) and the error message just says "error".
Works fine in the first one, when it happens to be on a page that the url parameter makes sense. Any idea why this is happening and what a solution might be?
Thanks in advance.
I have resolved the issue. The problem is being caused by the inclusion of domain name in the BASE_URL. The value used to be "http://www.mydomain.com/base/url/" in the BASE_URL variable. Once I dropped the "http://www.mydomain.com" it worked. So my BASE_URL became "/base/url/".
精彩评论