编程语言
首页 > 编程语言> > javascript – AJAX PageMethods与路由冲突?

javascript – AJAX PageMethods与路由冲突?

作者:互联网

编辑:Post底部的最新信息.

我在页面上有一个更新面板,我强制用__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.

解决方法:

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

PageMethods.GetLatestHeadlineTick(OnSucceeded,OnFailed);

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

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

PageMethods.set_path( ‘/ actualpath / actualpage.aspx’)

作品!

标签:javascript,net,ajax,url-routing,dopostback
来源: https://codeday.me/bug/20190903/1795876.html