views:

213

answers:

5

i'm getting an ajax output success data.

Where the data contains some html text and a script.

But the script is not executing, how can i execute the script.

lets say Ajax response obj is

"something....alert("test");"

the above code is my ajax response.The div is getting rendered, but the alert is not working.

A: 

Not sure if you are using a library, but with Prototype I had to set

evalScripts: true

before JavaScript would be eval-ed. See here for more info:

http://www.sergiopereira.com/articles/prototype.js.html#UsingAjaxRequest

RedFilter
A: 

Using jQuery here is a simple bit of code:

$.ajax({
    type: "POST",
    url: "getData.asmx/HelloWorld",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(result) {
        alert(result);
    }
});

But, to actually use the results of the variable result I ended up using a javascript library, from http://www.json.org/js.html, I did:

success: function(result) {
    var myData = JSON.parse(result.d);

There are probably better approaches, but this was simple, and worked for me, so I just use this. Later, when the project is in production I may go back and clean this up, but that is after I get everything working.

James Black
+1  A: 

If you are retrieving the JSON formatted result from AJAX call, you can just use eval to execute the javascript.

Assume, if the result json is formed like this

   var res =  '{"Data": "<something>",
              "script": "alert(something)"}';

   var out = eval("(" + res + ")");
   var data = out.data;
   eval(out.script);

Cheers

Ramesh Vel

Ramesh Vel
This is probably a bad idea. I could easily send my own JSON that could contain malicious javascript and it would execute without problems...
SpaDusA
A: 

Assuming you are not using JSON or jQuery, or any other library, and your AJAX call returns some HTML and/or javascript which is being added to your existing document (eg. using innerHTML), any javascript returned using AJAX will not execute in the browser - except for events on elements in the HTML.

So if your AJAX call returns <input type="button" value="Click me" onclick="alert('hello');" />, the js alert will work ok, but if your AJAX call returns <script type="text/javascript">alert('hello');</script> it will not execute. In that case, you would have to parse the result to extract the javascript and execute it, using a function such as this:

function extract_and_execute_js(output_to_parse)
{    
    if(output_to_parse != '')    
    {    
        var script = "";
        output_to_parse = output_to_parse.replace(/<script[^>]*>([\s\S]*?)<\/script>/gi, function(){if (output_to_parse !== null) script += arguments[1] + '\n';return '';});
        if(script) 
        {
            if (window.execScript)
            {
                window.execScript(script);
            }
            else
            {
                window.setTimeout(script, 0);
            }
        }
    }
}
Russ
A: 

Take a look at this article. The article is targetted at Grails but the concept can be applied at pretty much all languages and frameworks.

Oliver Weichhold