使用Ajax调用php文件时,php文件中的JavaScript不起作用

使用Ajax调用php文件时,php文件中的JavaScript不起作用

问题描述:

使用Ajax,我正在调用一个包含javascript的php文件,但是以这种方式,javaScript无法正常工作.

Using Ajax, I'm calling a php file that contains javascript, but in this way, the javaScript doesn't work.

main.html文件在此处给出.它只是使用Ajax调用名为test1.php的php文件来更新客户端的所有页面.

The main.html file is given here. It just uses Ajax for calling a php file called test1.php for updating all page at client.

 <!DOCTYPE html>
 <html>
 <body>
       <!-- run php file to fill the page -->
       <script>
             var xmlhttp = new XMLHttpRequest();

             xmlhttp.onreadystatechange=function()
             {
                 if (xmlhttp.readyState==4 && xmlhttp.status==200)
                 {
                    document.body.innerHTML = xmlhttp.responseText;         
                 }
             }

             xmlhttp.open("GET","test1.php",true);
             xmlhttp.send();
       </script> 
 </body>
 </html>

test1.php文件是非常简单的测试,如下所示:

And the test1.php file is very simple test as follows:

 <p id="demo">Hi there</p>
 <script>
        document.write("Yes! Hi there");    
        alert('Welcome!');
 </script>

现在,仅用于检查test1.php是否正常,我在浏览器的url行中输入了

Now, just for checking that test1.php is ok, I put in the browser's url line:

localhost/test1.php

一切正常,显示html和js的文本,并在警告窗口中显示欢迎词!显示.

and everything works ok, the text of the html and js are displayed and an alert window with the word Welcome! is displayed.

但是如果我调用主页

localhost/main.html

然后仅显示html文本"Hi there".JS被忽略.

Then only the html text 'Hi there' is displayed. The JS is ignored.

有人知道这是为什么吗?谢谢

Anybody knows why is this? Thanks

以下是3个关于ajax&可以做什么的基本示例.您应该如何做:

主要html

js

function ajax(a,b,c){ // Url, Callback, just a placeholder
 c=new XMLHttpRequest;
 c.open('GET',a);
 c.onload=b;
 c.send()
}

function changeHTML(){
 document.getElementById('mytext1').innerHTML=this.response;
}

function executeJS(){
 (new Function(this.response))()
}

var array=[];
function loadJSON(){
 array=JSON.parse(this.response);
 updateFields();
}

function updateFields(){
 for(var a=0,b;b=array[a];++a){
  document.getElementById(b.id).textContent=b.data;
 }
}

window.onload=function(){
 ajax('getHTML.php',changeHTML);
 ajax('getJS.php',executeJS);
 ajax('getJSON.php',loadJSON);
 // even if this works you should execute ajax sequentially
}

html

<div id="mytext1"></div>
<div id="mytext2"></div>


getHTML.php


getHTML.php

<?php
echo "<div>i'm html</div>";
?>

getJS.php

getJS.php

<?php
echo "var a='hello';alert(a);";
?>

getJSON.php

getJSON.php

<?php
$arr=array(array('id'=>'mytext2','data'=>'data'));
echo json_encode($arr);//json_decode
?>

如果要执行javascript函数,请使用 executeJS 函数并传递有效的javascript字符串.

if you want to execute a javascript function use the executeJS function and pass a valid javascript string.

不需要评估!

提示:使用json可以将函数作为数据传递,但是由于解析起初不喜欢字符串函数,一个不错的技巧是将javascript转换为base64,然后使用 atob将javascript转换回普通字符串(base64)并具有以下内容:

TIP:using json you can pass a function as data but as the parsing does not like the string function at the beginning a nice trick is to convert the javascript to base64 and back into a normal string with javascript with atob(base64) and have something like this:

function updateFields(){
 for(var a=0,b;b=array[a];++a){
  if(b.func){
   (new Function(atob(b.func)))()
  }else{
   document.getElementById(b.id).textContent=b.data;
  }
 }
}

php

<?php
$arr=array(
 array('id'=>'mytext2','data'=>'data'),
 array('func'=>base64_encode("alert('jsonfunc')"))
);
echo json_encode($arr);//json_decode
?>


也许有一些小错误...我现在写了.并且无法检查ajax atm.


maybe there are some little errors ... i wrote that now. and can't check ajax atm.

如果您有任何问题,只问!!

if you have any questions just ask !!