从.ashx文件调用Javascript方法

时间:2012-10-17 10:59:15

标签: javascript asp.net generic-handler

从我之前的问题(Create json using JavaScriptSerializer)开始,在.ashx文件中我使用以下方式打印json对象:

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

我从default.aspx调用此.ashx文件,该文件在其<head>标记内部有一些javascript函数。 我的问题是
如何在context.Response.Write(json);之后从.ashx文件调用javascript函数?

更新
我的最终目标是为DataTable实现Server Side Processing。我希望使用javascript函数将行绑定到上下文菜单。 为此,我使用以下代码来调用.ashx文件:

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

2 个答案:

答案 0 :(得分:1)

您使用的是ajax请求吗?在这种情况下,您可以使用javascript中提供的成功方法,如以下w3schools中的示例所示:

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

e.g。

$('#example').dataTable({
            'bProcessing': true,
            'bServerSide': true,
            'sAjaxSource': '/data.ashx',
            'fnInitComplete' : function() {
                alert('Your menu population code here!');
             }
        });

答案 1 :(得分:0)

您可以使用

eval

在客户端评估响应为javascript。但我怀疑你真的需要或想要这个,它可能不是一个非常优雅的解决方案。那你想要什么?