开发者

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.


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")');


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;';)


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>


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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜