[英]How to use jQuery AJAX in ASP.net master page?
I have an ASP.net web application with a master page. 我有一个带有母版页的ASP.net Web应用程序。 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. 但是,我决定使用jQuery AJAX和jQuery Autocomplete来使程序更容易使用。 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 . 该搜索在http://example.com/page1.aspx和http://example.com/page2.aspx中运行良好,但它无法在http://example.com/subdirectory/index.aspx中使用 。
Here is my javascript code to perform the autocomplete: (from the master page) 这是我执行自动完成的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;
}
});
It's a jQuery AJAX call to DeviceSelection.aspx/getDeviceFieldAutocomplete, a web service call in my ASP.net code. 这是对我的ASP.net代码中的Web服务调用DeviceSelection.aspx / getDeviceFieldAutocomplete的jQuery AJAX调用。 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 . DeviceSelection.aspx位于http://example.com/DeviceSelection.aspx ,所以我认为问题是当用户访问http://example.com/subdirectory/index.aspx并在查询中输入时,它尝试通过http://example.com/subdirectory/DeviceSelection.aspx调用Web服务。
How can I make this work? 我怎样才能做到这一点?
Change it to this: 把它改成这个:
...
$.ajax({
url: "/DeviceSelection.aspx/getDeviceFieldAutocomplete",
data: "{ 'text': '" + escape(request.term) + "', 'field': 'SerialNumber' }",
dataType: "json",
...
The "/" in front says that the URL is relative to the domain (rather than relative to the current page). 前面的“/”表示URL相对于域(而不是相对于当前页面)。
Can we get something relative to the virtual directory? 我们可以获得相对于虚拟目录的东西吗?
What about if you were to <%= Request.ApplicationPath %>
in the code for the URL and provide an absolute path? 如果您在URL的代码中使用<%= Request.ApplicationPath %>
并提供绝对路径,该怎么办? Similarly you could resolve the path using a client-resolver (I forget the exact syntax, but it's there) 类似地,您可以使用客户端解析器解析路径(我忘记了确切的语法,但它就在那里)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.