jQuery を使用して、これに似たシナリオを正常に実装しました。基本的に、私はbeforeSend
: 関数を使用して「お待ちください」タイプのページを表示します。これがプレイ中の基本的なコードです(そうではありません。AsyncController
基本クラスを使用してアクションを非同期にすることもできます):
<script type="text/javascript">
$(document).ready(function() {
$('#create').bind('click', function() {
saveFundProperty();
});
});
// main functions
function saveFundProperty() {
var url = '<%= Url.Action("Create", "FundProperty") %>';
var params = { fundId: $("#FundID").val(), propertyId: $("#PropertyID").val() };
SendAjax(url, params, beforeQuery, saveFundPropertyResponse);
}
function beforeQuery() {
var url = '<%= Url.Action("Wait", "FundProperty") %>';
$("#statusMsg").load(url);
}
function saveFundPropertyResponse(data) {
if (data.length != 0) {
if (data.indexOf("ERROR:") >= 0) {
$("#statusMsg").html(data).css('backgroundColor','#eeaa00');
}
else {
$("#statusMsg").html(data);
}
}
}
</script>
お役に立てれば。
このSendAjax
メソッドは、物事をもう少し一貫させるための純粋なラッパー関数です。ここに完全です:
<script type="text/javascript">
function SendAjax(urlMethod, jsonData, beforeSendFunction, returnFunction, dataType, contentType) {
$.ajaxSetup({ cache: false });
dataType = dataType || "text"; // default return type
contentType = contentType || "application/x-www-form-urlencoded"; // default input type
$.ajax({
type: "POST",
url: urlMethod,
data: jsonData,
dataType: dataType,
contentType: contentType,
beforeSend: function() {
if(beforeSendFunction!==null)
beforeSendFunction();
},
success: function(data) {
// Do something interesting here.
if (data != null && returnFunction!==null) {
returnFunction(data);
}
},
error: function(xhr, status, error) {
// Boil the ASP.NET AJAX error down to JSON.
var err = eval("(" + xhr.responseText + ")");
// Display the specific error raised by the server
alert(err.Message);
}
});
}
</script>
[編集] - SendAjax フォーマットで何が起こっているのかわかりません。簡単にコピー/貼り付けできることを願っています...