0

I'm using an ajax call to append a MVC partial view with some styles sheets and script files to my php page.

However it is not appending de <script> tags. I already checked my HTTP request on the network and it really brings those tags.

My code:

$.ajax({
    type: 'POST',
    url: 'http://localhost:63322/MyController/MyAction', //external url project
    data: JSON.stringify(parameters),
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    async: true,
    crossDomain: true,
    processdata: true,
    headers: {
        "Access-Control-Allow-Origin" : "*",
        "Access-Control-Allow-Headers": "*"
    },
    success: function(result){  
        $(".pageContainer").html(result);           
    },
    error: function(er){ alert('Error'); }
});

On ajax success function I already tried:

  • to use $(".pageContainer").empty().append(result)
  • to separate the script tags and add to <head> like this:
 var elems = $(result);
 var scripts = $.grep(elems, function(e){ 
      try{ 
           return e.tagName.toLowerCase() == "script";
      }catch(er){ return false; }
  });
 var remainElems = $.grep(elems, function(e){ 
      try{ 
           return e.tagName.toLowerCase() != "script";
      }catch(er){ return false; }
 });

 $.each(scripts, function(){ $('head')[0].appendChild(this); });
 $(".pageContainer").append(remainElems);
  • to give some time before appending with setTimeout(function(){ $(".pageContainer").html(result); }, 1000);

  • to change <script> tags to <link type="text/javascript" src="http://someurl.com" rel="tag"/> and it was appended but the code wasn't executed

But nothing works.

What is wrong? What I'm missing?

My PHP page uses jquery-1.8.3 and jquery-ui-1.9.2.custom. This is the problem?

NOTE:

My question is very different from that on: Executing inside retrieved by AJAX

If you read both you will see they are very different. I already readed what and noticed that.

Ninita
  • 1,209
  • 2
  • 19
  • 45
  • 1
    The problem is, as have many others encountered before you, that appending a ` –  Feb 16 '18 at 14:06
  • @ChrisG if you read all my question you will see I already tried that but that doesn't worked. – Ninita Feb 16 '18 at 14:15
  • @ChrisG yes, was what I said. And yes, I already sayed `I already checked my HTTP request on the network and it really brings those tags` – Ninita Feb 16 '18 at 14:23

1 Answers1

0

Solved. I don't know why but seems jquery-1.8.3 don't performs the insertion of the <script> tags to the html code dynamically.

I changed

<script type="text/javascript" src="js/jquery-1.8.3.js"></script>

to

<script type="text/javascript" src="js/jquery-1.10.2.js"></script>

and now it works.

Ninita
  • 1,209
  • 2
  • 19
  • 45