0

I have page, where I'm making an ajax call to PageMethod, and as a result I'm getting some html string, which I need to append to some div. Now Html string has some javascript code in it, so I expect this js code to execute, after I append html to div. But the problem is, I'm getting error saying that - Microsoft JScript runtime error: Access is denied., this error gets thrown when globalEval method tries to execute.

This is an ajax call I make

var data ={id: id};

$.ajax({

    type: "POST",
    url: "/srv/Loader.aspx/LoadUserControl",
    contentType: "application/json; charset=utf-8",
    data: JSON.stringify(data),
    dataType: "json",
    success: function (data) {
       $('#TargetDivID').html(data.d);
    },
    error: function (error) {
       $('#TargetDivID')..html("Control can not be loaded");
       console.log("LOG: Exeption trying to load Control:\n" + error.responseText);
    }
});

Here is the Html returned by WebMethod:

<div class="someclass" id="inHouseAdModule" style="display:none;">
Some Html Content
    <script type="text/javascript">
        $(document).ready(function () {
            Some logic goes here
        });
    </script>

</div>

Any ideas how can I fix this ?

EDIT:

 <div class="someclass" id="inHouseAdModule" style="display:none;">
    Some Html Content
        <script type="text/javascript">            
                Some logic goes here            
        </script>
 </div>

Does not work as well.

EDIT: I've added debugger at the beginning of JS code, but exception was thrown before reaching that point.

<script type="text/javascript"> 
   debugger;           
   Some logic goes here            
</script>
2
  • Do you have control over the returned HTML/JS? Could you for example break it up in to one JS section and one HTML section? I'm not sure whether the document ready event will even fire for your JS to work since it likely already fired before the AJAX call took place. Maybe someone else can confirm the behaviour in this case.
    – Vala
    Aug 21, 2012 at 13:21
  • I've updated the post, please take a look. Still doesn't work.
    – Gev
    Aug 21, 2012 at 14:41

1 Answer 1

0

After searching your error, the general consensus is that you probably have an incorrect URL in your request. If the web method accepts GET, try to hit the URL in your browser to make sure you have it right. Additionally, you can check the network traffic with your browser's developer tools to make sure your request and the response has the expected headers.

============================================================

Additionally

The document is likely already ready. Change your HTML to the following to execute the JavaScript immediately.

<script type="text/javascript"> /*Some logic goes here*/ </script>

If you need to call it again later, keep the function wrapper and call the function right after its declaration.

<script type="text/javascript"> function MyFunc() { Some logic goes here } MyFunc(); </script>

============================================================

An Alternative Approach

Instead of loading dynamically created content via AJAX and trying to inject it onto your page, I recommend using an HTML IFRAME that points to a URL that returns the dynamically crated content. This is a common approach for advertisement content.

11
  • Thanks for suggestion, I've already tried this, it doesn't work.
    – Gev
    Aug 21, 2012 at 14:38
  • I'm getting back expected results in success function of my ajax call, I just can't execute js code.
    – Gev
    Aug 22, 2012 at 6:45
  • @Gev, have you tried running in an alternate browser? That should help determine if it is a browser configuration issue. It may yield better error messages as well.
    – Malgaur
    Aug 22, 2012 at 13:51
  • @Gev, try it without wrapping your HTML with JSON. Send it by itself with a content type of 'text/html'. Then just shove the response text of the AJAX call right into the DIV.
    – Malgaur
    Aug 22, 2012 at 13:52
  • 10x Malgaur, tried, didn't help :( It seems this is happening when some AJAX calls are made simultaneously. What do you think?
    – Gev
    Aug 22, 2012 at 15:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.