gpt4 book ai didi

javascript - AJAX PageMethods 与路由冲突?

转载 作者:行者123 更新时间:2023-11-29 09:58:29 28 4
gpt4 key购买 nike

编辑:帖子底部的最新信息。

我在使用 __doPostBack 强制回发的页面上有一个更新面板。

当我在 /path/page.aspx 浏览它时一切正常。

但是,一旦我通过像 /otherpath/page 这样的路径访问页面,回发就不会发生。

有什么建议吗?

这是我的 JS 文件:

/// <reference name="MicrosoftAjax.js"/>
function Check() {
// Call the static page method.
PageMethods.GetLatestHeadlineTick(OnSucceeded, OnFailed);
}

function OnSucceeded(result, userContext, methodName) {
// Parse the page method's result and the embedded
// hidden value to integers for comparison.
var LatestTick = parseInt(result);
var LatestDisplayTick = parseInt($get('LatestDisplayTick').value);

// If the page method's return value is larger than
// the embedded latest display tick, refresh the panel.
if (LatestTick > LatestDisplayTick)
__doPostBack('UpdatePanel1', '');
// Else, check again in five seconds.
else
setTimeout("Check()", 5000);
}

// Stub to make the page method call happy.
function OnFailed(error, userContext, methodName) { }

function pageLoad() {
// On initial load and partial postbacks,
// check for newer articles in five seconds.
setTimeout("Check()", 5000);
}

还有我的标记:

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="True">
<Scripts>
<asp:ScriptReference Path="/resources/js/bus-times.js" />
</Scripts>
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" ClientIDMode="Static">
<ContentTemplate>
<asp:GridView ID="gvSchedule" runat="server" AutoGenerateColumns="False" Width="80%">
<AlternatingRowStyle CssClass="altrowstyle" />
<HeaderStyle CssClass="headerstyle" />
<RowStyle CssClass="rowstyle" />
<Columns>
<asp:BoundField DataField="RouteName" HeaderText="Route" />
<asp:BoundField DataField="TimeTillDeparture" HeaderText="Departs In" />
<asp:BoundField DataField="ScheduledDepartureTime" HeaderText="Est. Departure Time" />
</Columns>
<EmptyDataTemplate>
Data is currently unavailable.
</EmptyDataTemplate>
</asp:GridView>
<div class="updatedstyle">
Last updated:
<asp:Label ID="updated_time" runat="server" ></asp:Label></div>
<asp:HiddenField runat="server" ID="LatestDisplayTick" ClientIDMode="Static" />
<asp:HiddenField runat="server" ID="hf_stopID" ClientIDMode="Static" />
</ContentTemplate>
</asp:UpdatePanel>

以及代码隐藏中的 Ajax 方法:

<WebMethod()> _
Public Shared Function GetLatestHeadlineTick() As Long

Dim stopID As String
If HttpContext.Current.Request.QueryString("stop_id") <> Nothing Then
stopID = HttpContext.Current.Request.QueryString("stop_id")
Else
stopID = "678036"
End If

' Retrieve the cached DataTable.
Dim dt_added As DateTime = CType(BusScheduleService.GetBusDataDateAdded(stopID), DateTime)

' Return that bus data timestamp, in ticks.
Return dt_added.Ticks

End Function

编辑:

这是来自 Fiddler 的图片。工作版本在顶部,错误在底部。它返回一个 405 请求。因此,似乎 Ajax 请求在解析时被解释为实际路由名称,但该路由不存在,因此无法正常工作。我该如何解决这个问题?似乎当 Ajax 调用一个函数时,它通过在 URL 后面指定一个/functionName 来调用它,但这模仿了路由语法......

Fiddler Output

因此,当 AJAX 尝试通过/path/page.aspx/GetLatestHeadLineTick 调用 GetLatestHeadLineTick 时,它会起作用。但是通过一条路线,它会转到/otherpath/page/GetLatestHeadLineTick,我想我的网站正在尝试将其作为路线而不是 AJAX 请求来处理。

我还注意到请求有效,它说内容类型是 JSON,但在失败的请求中它被解释为 HTML。

最佳答案

好吧,我解决了这个问题,花了很长时间才找到真正的原因,但是路由与 __doPostBack 或 AJAX 函数调用没有冲突。问题是 PageMethods 类和路由之间存在冲突。

PageMethods.GetLatestHeadlineTick(OnSucceeded, OnFailed);

上面一行查看路由并尝试从路由中获取页面方法,这不起作用。

所以我需要做的就是在那一行之前添加这一行:

PageMethods.set_path('/actualpath/actualpage.aspx')

有效!

关于javascript - AJAX PageMethods 与路由冲突?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6699404/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com