简体   繁体   中英

Need a javascript function in return of Ajax Call

I am trying to get the javascript function in ajax call. My code works fine and returning back my javascript code but there is a problem in my received javascript function. Here is my code for Ajax..

function media_type(id)
{
    var xmlhttp;
    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)
                {
                    eval(document.getElementById("asad").innerHTML=xmlhttp.responseText);
                }
            }
    xmlhttp.open("GET","video/media_type.php?cat_id="+id,true);
    xmlhttp.send();
    }

And her is the code where i am returning a simple hello world alert box.

    if($num_rows==0)
    {
    //echo("Sorry no record found.");

    echo'<script type="text/javascript">
                alert("hello World");
                </script>';
    }

I am receiving the code back exactly but code is not executing. Please tell me how i can fix this issue. Thanks in advance.

Can I ask why you need to return JavaScript from the server? How about you return a simple response (string, boolean, number) from the server, and execute something that already lives on the client based on the return value? Much safer and easier. It may not be the solution you are looking for, but your js-fu will be much stronger for it.

I had this issue the other day...

you have to set your header in the php page to Content-Type: text/javascript; charset=utf-8

Here is the Entire Code to make this work

For your php file

<?php 
header("Content-Type: text/javascript; charset=utf-8");
echo'<script type="text/javascript">
            alert("hello World");
            </script>
      ';?>

and here is the Javascript

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js" type="text/javascript"></script>
        <script>
$.ajax({
        type:'POST',
        url:'toget.php',
        success:function(d)
        {
            $(document).append(d);
        }
        });
        </script>

i will try with this

if($num_rows==0)
{
//echo("Sorry no record found.");
 echo'<html><script type="text/javascript">
            alert("hello World");
            </script>
      </html>';
}

i think this line is creating problem

eval(document.getElementById("asad").innerHTML=xmlhttp.responseText)

that is

eval(document.getElementById("asad").innerHTML='<script>alert("Hi");</script>';)

getting converted to

eval(document.getElementById("asad").innerHTML='&lt; script &gt; alert("Hi");&lt;/script&gt;';)

Just return the JS that you want to execute ( alert("hello World"); ) and make an eval . Remove <script type="text/javascript"> and </script>

eval(xmlhttp.responseText);

will be:

eval('alert("hello World")');

//try using this
<?php
  if($num_rows==0)
    {

    //echo("Sorry no record found.");
?>
  <script type="text/javascript">
            alert("hello World");
            </script>
<?php
    }
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM