In my ASP.net web project, I've written the following Javascript code in a .js file:
function getDeviceTypes() {
var deviceTypes;
$.ajax({
async: false,
type: "POST",
url: "Controls/ModelSelectorWebMethods.aspx/getDeviceTypes",
data: '{ }',
contentType: "application/json;",
dataType: "json",
success: function(response) {
deviceTypes = response.d;
},
error: function(xhr, status) {
debugger;
alert('Error getting device types.');
}
}); // end - $.ajax
return deviceTypes;
}
It was working great until I tried to load this .js file into a page in a subdirectory.
Let's suppose that the name of my project is widget
.
When I use this code in the main virtual directory, Javascript interprets Controls/ModelSelectorWebMethods.aspx/getDeviceTypes
to mean https://mysite.com/widget/Controls/ModelSelectorWebMethods.aspx/getDeviceTypes
and all is well. However, from the page in a subdirectory, Javascript interprets it to mean https://mysite.com/widget/subdirectory/Controls/ModelSelectorWebMethods.aspx/getDeviceTypes
and it doesn't work.
How can I write my Javascript code so that the AJAX web method can be called from pages in any directory in my application?
/widget/Controls/...
async: false
, your users will hate you for locking up their browsers. Specify a callback instead. The only resource I have off hand is stackoverflow.com/questions/2956261/…, but a quick Google for AJAX callback will help you loads.