2

私の以前の質問 ( JavaScriptSerializer を使用して json を作成する) から、.ashxファイルでは、次を使用して json オブジェクトを出力しています。

context.Response.ContentType = "application/json";
context.Response.Write(json);

タグ .ashx内にいくつかのjavascript関数があるdefault.aspxからこのファイルを呼び出しています。私の質問は次 のとおりです。.ashxファイルからjavascript関数を呼び出すにはどうすればよいですか? <head>
context.Response.Write(json);

更新:
私の最終的な目標は、DataTable のサーバー側処理を実現することです。そのため、javascript 関数を使用してコンテキスト メニューで行をバインドしたいと考えています。そのために、次のコードを使用して.ashxファイルを呼び出しています。

 $('#example').dataTable({
            'bProcessing': true,
            'bServerSide': true,
            'sAjaxSource': '/data.ashx'
        });
4

2 に答える 2

1

ajax リクエストを使用していますか? その場合、次の w3schools の例のように、JavaScript で利用できる success メソッドを使用できます。

function showHint(str)
{
var xmlhttp;
if (str.length==0)
  { 
  document.getElementById("txtHint").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    // You can call your custom method here...  
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","gethint.asp?q="+str,true);
xmlhttp.send();

}

または、jquery を使用している場合:

$.ajax({
  url: "test.html",
  context: document.body
}).done(function() { 
  // You can call your custom method here... 
  $(this).addClass("done");
});

アップデート

チェックアウト: http://datatables.net/usage/callbacks使用できるメソッドは次のとおりです: fnInitComplete

例えば

$('#example').dataTable({
            'bProcessing': true,
            'bServerSide': true,
            'sAjaxSource': '/data.ashx',
            'fnInitComplete' : function() {
                alert('Your menu population code here!');
             }
        });
于 2012-10-17T11:14:58.893 に答える