Calling javascript function inside jquery ajax res

2019-08-15 09:06发布

问题:

I am trying to call date_cal() javascript function inside ajax response (wall_list.php).Every thing is fine am getting correct response. But its not calling date_cal() function.

main file:

$.ajax({

  url: 'wall_list.php',
  data:"dt_from="+dt_from+"&dt_to="+dt_to+"&week="+week+"&month="+month+"&dt_filter="+dt_filter+"&fan="+fan+"&gender="+gender+"&pageNumber="+pagenumber,
  type: 'POST',
success: function (resp) { 

if(resp)
{
 //alert(resp);
  document.getElementById('wall_listdiv').innerHTML=resp;

}  

Wall_list.php

Some code...................

>   <td id="<?php print $key; ?>" class="tm_td" valign="top" colspan=2>
>   

    <script language="JavaScript">
                                date_cal('<?php print $commentcreatetimearr[$key]; ?>','<?php print $key; ?>');
                                </script>

>       </td>

Some code......................

it's not calling javascript there.

Can anyone explain how to all this function in response.

回答1:

for example
php:

<?php echo $commentcreatetimearr[$key]; ?>

js:

$.ajax({    
  url: 'wall_list.php',
  data:"dt_from="+dt_from+"&dt_to="+dt_to+"&week="+week+"&month="+month+"&dt_filter="+dt_filter+"&fan="+fan+"&gender="+gender+"&pageNumber="+pagenumber,
  type: 'POST',
success: function (resp) {     
  if(resp){
     $('#wall_listdiv').html(date_cal(resp));
  }  


回答2:

here you go

$.ajax({
    url: 'wall_list.php',
    data: "dt_from="+dt_from+"&dt_to="+dt_to+"&week="+week+"&month="+month+"&dt_filter="+dt_filter+"&fan="+fan+"&gender="+gender+"&pageNumber="+pagenumber,
    type: 'POST',
    success: function (resp){
        if(resp){
            $("#wall_listdiv").html(resp);
        }
    },
    dataType: 'html'
});

What you want to do is, specify the return dataType as html. From jQuery API

If html is specified, any embedded JavaScript inside the retrieved data is executed before the HTML is returned as a string. Similarly, script will execute the JavaScript that is pulled back from the server, then return the script itself as textual data.

More information here: jQuery.ajax() - jQuery API