如何在 ASP.net 母版页中使用 jQuery AJAX?

发布于 2024-10-07 15:54:06 字数 2391 浏览 1 评论 0原文

我有一个带有母版页的 ASP.net Web 应用程序。在我的母版页的菜单栏中,有一个搜索功能,用户可以在其中输入查询并单击按钮。单击该按钮后,用户的浏览器将导航到显示搜索结果的页面。这个功能效果很好。

不过,我决定使用 jQuery AJAX 和 jQuery Autocomplete 来使程序更易于使用。从 http://example.com/page1.aspxhttp://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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你好,陌生人 2024-10-14 15:54:06

改成这样:

...
$.ajax({
    url: "/DeviceSelection.aspx/getDeviceFieldAutocomplete",
    data: "{ 'text': '" + escape(request.term) + "', 'field': 'SerialNumber' }",
    dataType: "json",
...

前面的“/”表示该URL是相对于域名的(而不是相对于当前页面的)。

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).

烟沫凡尘 2024-10-14 15:54:06

我们可以获得与虚拟目录相关的内容吗?

如果您要在 URL 代码中使用 <%= Request.ApplicationPath %> 并提供绝对路径,会怎么样?同样,您可以使用客户端解析器解析路径(我忘记了确切的语法,但它就在那里)

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? Similarly you could resolve the path using a client-resolver (I forget the exact syntax, but it's there)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文