在返回Ajax调用需要一个javascript函数函数、Ajax、javascript

2023-09-10 19:00:58 作者:花开时花落、

我试图让AJAX调用JavaScript函数。我的code正常工作并返回回我的javascript code,但没有在我收到的JavaScript函数的一个问题。这是我的code为Ajax。

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();
    }

和她是code其中我返回一个简单的Hello World警告框。

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

我收到了code回去,但究竟code不执行。请告诉我,我怎么能解决这个问题。先谢谢了。

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

推荐答案

我可以问你为什么需要从服务器返回的JavaScript?你怎么样从服务器返回一个简单的回应(字符​​串,布尔,数字),并执行一些基于返回值已经住在客户端上?更安全,更容易。它可能不是你正在寻找的解决方案,但你的JS福将它强多了。

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.