如何在 ASP.net 母版页中使用 jQuery AJAX?
我有一个带有母版页的 ASP.net Web 应用程序。在我的母版页的菜单栏中,有一个搜索功能,用户可以在其中输入查询并单击按钮。单击该按钮后,用户的浏览器将导航到显示搜索结果的页面。这个功能效果很好。
不过,我决定使用 jQuery AJAX 和 jQuery Autocomplete 来使程序更易于使用。从 http://example.com/page1.aspx 和 http://example.com/page2.aspx,但它在 http://example.com/subdirectory/index.aspx。
下面是我执行自动完成的 javascript 代码:(来自母版页)
function setupSerialNumberAutocomplete(id) {
$(id).autocomplete({
source: function(request, response) {
$.ajax({
url: "DeviceSelection.aspx/getDeviceFieldAutocomplete",
data: "{ 'text': '" + escape(request.term) + "', 'field': 'SerialNumber' }",
dataType: "json",
type: "POST",
contentType: "application/json",
dataFilter: function(data) { return data; },
success: function(data) {
response($.map(data.d, function(item) {
return {
value: item
}
}))
},
error: function(xhr, status) {
var exception = eval("(" + xhr.responseText + ")");
$("#divStatus").html("Error fetching registration codes list: " + xhr.statusText + " - " + exception.Message + ".");
}
}); //end - ajax
},
minLength: 2,
focus: function(event, ui) {
$(id).val(ui.item.value);
return false;
},
select: function(event, ui) {
$(id).val(ui.item.value);
return false;
}
});
这是对 DeviceSelection.aspx/getDeviceFieldAutocomplete 的 jQuery AJAX 调用,这是我的 ASP.net 代码中的 Web 服务调用。 DeviceSelection.aspx 位于 http://example.com/DeviceSelection.aspx,所以我认为问题是,当用户访问 http://example.com/subdirectory/index.aspx 并输入查询后,它会尝试调用 http://example.com/subdirectory/DeviceSelection 处的 Web 服务。 .aspx.
我怎样才能做到这一点?
I have an ASP.net web application with a master page. In the menu bar of my master page, there is a search function where a user types in a query and clicks a button. When the button is clicked, the user's browser navigates to a page that shows search results. This funcitonality works great.
However, I decided to use jQuery AJAX and jQuery Autocomplete to make the program easier to use. The search works fine from http://example.com/page1.aspx and http://example.com/page2.aspx, but it doesn't work from http://example.com/subdirectory/index.aspx.
Here is my javascript code to perform the autocomplete: (from the master page)
function setupSerialNumberAutocomplete(id) {
$(id).autocomplete({
source: function(request, response) {
$.ajax({
url: "DeviceSelection.aspx/getDeviceFieldAutocomplete",
data: "{ 'text': '" + escape(request.term) + "', 'field': 'SerialNumber' }",
dataType: "json",
type: "POST",
contentType: "application/json",
dataFilter: function(data) { return data; },
success: function(data) {
response($.map(data.d, function(item) {
return {
value: item
}
}))
},
error: function(xhr, status) {
var exception = eval("(" + xhr.responseText + ")");
$("#divStatus").html("Error fetching registration codes list: " + xhr.statusText + " - " + exception.Message + ".");
}
}); //end - ajax
},
minLength: 2,
focus: function(event, ui) {
$(id).val(ui.item.value);
return false;
},
select: function(event, ui) {
$(id).val(ui.item.value);
return false;
}
});
It's a jQuery AJAX call to DeviceSelection.aspx/getDeviceFieldAutocomplete, a web service call in my ASP.net code. DeviceSelection.aspx is located at http://example.com/DeviceSelection.aspx, so I presume that the problem is that when a user accesses http://example.com/subdirectory/index.aspx and types in a query, it tries to call the web service at http://example.com/subdirectory/DeviceSelection.aspx.
How can I make this work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
改成这样:
前面的“/”表示该URL是相对于域名的(而不是相对于当前页面的)。
Change it to this:
The "/" in front says that the URL is relative to the domain (rather than relative to the current page).
如果您要在 URL 代码中使用
<%= Request.ApplicationPath %>
并提供绝对路径,会怎么样?同样,您可以使用客户端解析器解析路径(我忘记了确切的语法,但它就在那里)What about if you were to
<%= Request.ApplicationPath %>
in the code for the URL and provide an absolute path? Similarly you could resolve the path using a client-resolver (I forget the exact syntax, but it's there)