Question

From my previous question(Create json using JavaScriptSerializer), In .ashx file I am printing the json object using:

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

I am calling this .ashx file from default.aspx which has some javascript function inside its <head> tag. My question is :
How will I be able to call the javascript function from .ashx file after context.Response.Write(json);?

UPDATE:
My ultimate goal is to achieve Server Side Processing for DataTable.In that i want to bind the rows with context menu using javascript function. For that I am using following code to call .ashx file:

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

Solution

Are you using ajax requests? In that case, you can use the success method that is available in javascript, as in the following example from 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();

}

Or if you are using jquery:

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

UPDATE

Check out: http://datatables.net/usage/callbacks The method you can use is: fnInitComplete

e.g.

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

OTHER TIPS

You could use

eval

to evaluate the response as javascript on client-side. But I doubt you really need or want this, it might not be a very elegant solution. So what you want to archieve?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top