3

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

我在一个页面上有一个更新面板,我用 __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 来执行此操作,但这模仿了路由语法......

提琴手输出

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

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

4

1 回答 1

6

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

PageMethods.GetLatestHeadlineTick(OnSucceeded, OnFailed);

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

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

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

作品!

于 2011-07-15T17:52:00.690 回答