編集: 投稿の下部にある最新情報。
ページに __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
編集:
これはフィドラーからの写真です。上が動作中のバージョン、下がエラーです。405 リクエストを返します。そのため、Ajax リクエストは解決時に実際のルート名として解釈されているようですが、そのルートが存在しないため機能していません。どうすればこれを回避できますか? Ajax が関数を呼び出すとき、URL の後に /functionName を指定して呼び出しているようですが、これはルート構文を模倣しています...
したがって、AJAX が /path/page.aspx/GetLatestHeadLineTick を介して GetLatestHeadLineTick を呼び出そうとすると、機能します。しかし、ルートを使用すると、/otherpath/page/GetLatestHeadLineTick に移動します。これは、私のサイトがそれを AJAX 要求ではなくルートとして処理しようとしていると思います。
また、機能するリクエストでは、コンテンツ タイプが JSON であることがわかりますが、失敗したリクエストでは HTML として解釈されています。