【发布时间】:2011-09-12 17:29:30
【问题描述】:
在我的 ASP.net Web 项目中,我在 .js 文件中编写了以下 Javascript 代码:
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;
}
在我尝试将此 .js 文件加载到子目录中的页面之前,它一直运行良好。
假设我的项目名称是widget。
当我在主虚拟目录中使用此代码时,Javascript 将Controls/ModelSelectorWebMethods.aspx/getDeviceTypes 解释为https://mysite.com/widget/Controls/ModelSelectorWebMethods.aspx/getDeviceTypes,一切都很好。但是,从子目录中的页面中,Javascript 将其解释为 https://mysite.com/widget/subdirectory/Controls/ModelSelectorWebMethods.aspx/getDeviceTypes 并且它不起作用。
如何编写我的 Javascript 代码,以便可以从应用程序中任何目录中的页面调用 AJAX Web 方法?
【问题讨论】:
-
你试过绝对路径吗?
/widget/Controls/... -
我想我可以使用绝对路径,但这会破坏我不使用虚拟目录 /widget 的开发服务器...
-
请不要使用
async: false,您的用户会讨厌您锁定他们的浏览器。改为指定回调。我手头唯一的资源是stackoverflow.com/questions/2956261/…,但快速的 Google for AJAX 回调将帮助您加载。
标签: javascript jquery asp.net ajax