AJAX返回的JavaScript不起作用。如何使用JavaScript?

时间:2022-03-13 03:55:15

this is my page Test1.asp

这是我的页面Test1.asp

<html>
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>New Page 1</title>
<script type="text/javascript">
function Alex()
{
var xmlHttp;
try
  {  
  xmlHttp=new XMLHttpRequest();  }
catch (e)
  { 
   try
    {    
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");   
     }
  catch (e)
    {   
     try
      {     
       xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");   
          }
    catch (e)
      {      
      alert("Your browser does not support AJAX!");      
      return false; 
           }    
           } 
            }
  xmlHttp.onreadystatechange=function()
    {
    if(xmlHttp.readyState==4)
      {
      document.getElementById("Alex").innerHTML =xmlHttp.responseText;//Get Google Destination Map 
      }
    }
    xmlHttp.open("GET","Test2.asp" ,true);
    xmlHttp.send(null); 
   }
</script>
</head>

<body>
<div id ="Alex"></div>
<label onclick="Alex()" >ssss</label>
</body>
</html>

This is requested page Test2.asp

这是请求页面Test2.asp

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>New Page 1</title>
</head>
<body>
<div id="Mathew"></div>
</body>
<script type="text/javascript" >
{
document.getElementById("Mathew").innerHTML='ajax is working';      
}
</script>
</html>

In the page (Test2.asp) javascript is not working

在页面(Test2.asp)中javascript无法正常工作

How do i call test2.asp to my test1.asp using ajax

如何使用ajax将test2.asp调用到我的test1.asp

6 个解决方案

#1


0  

By default JavaScript contained with AJAX responses is not executed.

默认情况下,不会执行包含AJAX响应的JavaScript。

There is no point in building an Ajax handler from scratch when this problem has already be solved in various libraries just as jQuery and Prototype.

当这个问题已经在各种库中解决时,就像jQuery和Prototype一样,从头开始构建Ajax处理程序毫无意义。

#2


1  

In HTML inserted by Javascript does not execute automatically (at least in IE for sure). The only solution to this is to gather each of the script blocks in the loaded HTML and evaluate them each.

在Javascript插入的HTML中不会自动执行(至少在IE中是肯定的)。解决此问题的唯一方法是在加载的HTML中收集每个脚本块并对每个脚本块进行评估。

EDIT

编辑

I am using YUI here... the Dom class can collect all script tags from within the given block.

我在这里使用YUI ...... Dom类可以从给定的块中收集所有脚本标签。

var domElement = document.getElementById("Alex");
var scriptBlocks = YAHOO.util.Dom.getElementsBy(function() {return true;},'script',domElement);
for (var i = 0 ; i < scriptBlocks.length ; ++i){
    eval(scriptBlocks[i].innerHTML);
}

Simple as that. Also becareful about Internet Explorer... if you load in HTML using ajax, and it comes back with the script block as one of the first elements, it will, for some odd reason, ignore the script block and not include it in the response. To fix it, put a div above the script block with text in it with a style attribute of display:none;

就那么简单。同样考虑到Internet Explorer ...如果你使用ajax加载HTML,并且它以脚本块作为第一个元素之一返回,由于某些奇怪的原因,它将忽略脚本块而不包括在响应中。要修复它,请在脚本块上方放置一个div,其中包含一个样式属性为display:none;

If this is the HTML returned to IE, it will not include the script block in the response

如果这是返回给IE的HTML,则它不会在响应中包含脚本块

<div>
   <script type="text/javascript">
     /* Some javascript */
   </script>
</div>

This will fix the issue

这将解决问题

<div style="display:none;">some text</div>
<div>
   <script type="text/javascript">
     /* Some javascript */
   </script>
</div>

Very weird, but thats how IE rolls.

非常奇怪,但那就是IE如何滚动。

#3


0  

Use an absolute URI instead of a relative URL.

使用绝对URI而不是相对URL。

#4


0  

Adding a <script> element into a document via innerHTML doesn't(*) execute its contents as a script.

通过innerHTML将

You're also trying to insert the entire HTML document, including <html>, <head> and <body> inside a <div>, which is quite invalid.

您还试图在

中插入整个HTML文档,包括,和,这是非常无效的。

If you need to return both HTML and some script to execute, better to return a JSON object, eg.:

如果你需要返回HTML和一些脚本来执行,最好返回一个JSON对象,例如:

{
    "html": "<div id="Mathew"></div>",
    "js": "document.getElementById(\"Mathew\").innerHTML='ajax is working';"
}

then parse the JSON object, set the innerHTML to obj.html and eval the js. (Though it's generally questionable to be returning and executing arbitrary, scripts, there can sometimes be a use for it.)

然后解析JSON对象,将innerHTML设置为obj.html并评估js。 (虽然返回并执行任意脚本通常是有问题的,但有时可以使用它。)

(*: Well, doesn't generally. Exactly when a <script> element's contents get executed is browser-dependent. For example Firefox executes script when you append/insert a DOM HTMLScriptElement or ancestor into an element that is part of the document, whereas IE executes it when you insert the element into any parent for the first time, whether inside the document or not. In general, avoid inserting JavaScript-in-HTML content into HTML.)

(*:嗯,通常不会。确切地说,

#5


0  

Your methodology is slightly amiss. Typically, AJAX is used to send and receive data in non-HTML formats, such as XML, JSON, or sometimes even CSV (however, HTML is sometimes returned to the client, but usually as pieces of a page, not entire pages as in your example).

你的方法略有不妥。通常,AJAX用于以非HTML格式发送和接收数据,例如XML,JSON,有时甚至是CSV(但是,HTML有时会返回到客户端,但通常作为页面的一部分,而不是整个页面,如你的例子)。

Logic is rarely transmitted and is usually maintained on the respective sides of the transmission. In other words, the client/request side has all of its own logic and already knows what to do with the data returned from the server/response side (which also doesn't accept or require any logic generated from the client side). Further, the use of eval, which is usually necessary to consistently execute the logic found in the response, is generally frowned upon and considered a bad practice, thus the saying, "eval is evil."

逻辑很少传输,通常保持在传输的各个侧面。换句话说,客户端/请求方具有其自己的所有逻辑,并且已经知道如何处理从服务器/响应方返回的数据(其也不接受或要求从客户端生成的任何逻辑)。此外,使用eval(通常是必须始终如一地执行响应中的逻辑)通常不赞成并被认为是一种不好的做法,因此称之为“eval是邪恶的”。

In some cases, it may be necessary, advantageous or just plain easier to receive logic as part of the response from the server. In these situations however, it is still considered a best practice to separate your data from your logic.

在某些情况下,作为来自服务器的响应的一部分,接收逻辑可能是必要的,有利的或更简单的。但是,在这些情况下,将数据与逻辑分开仍然是最佳做法。

All that to nicely say that you're doing it wrong. I encourage you to read up on how AJAX works and how best to use it: w3schools walk-through, Mozilla MDC intro, AJAX and XML processing, updating a page (similar to what I think you're trying to do), and finally, AJAX API docs for jQuery, Prototype and Dojo.

所有这些都很好地说你做错了。我鼓励您阅读AJAX如何工作以及如何最好地使用它:w3schools演练,Mozilla MDC简介,AJAX和XML处理,更新页面(类似于我认为你想要做的),最后,jQuery,Prototype和Dojo的AJAX API文档。

#6


0  

Prototype has a nice way of handling this. See their stripScripts, extractStrips, and evalScripts methods on the String object. If you just strip the scripts, put your text into a div and then evalScripts, that'll work across all brwosers so scripts get executed exactly once.

Prototype有一个很好的处理方式。在String对象上查看stripScripts,extractStrips和evalScripts方法。如果您只是删除脚本,将文本放入div,然后再放入evalScripts,这将适用于所有brwosers,因此脚本只执行一次。

#1


0  

By default JavaScript contained with AJAX responses is not executed.

默认情况下,不会执行包含AJAX响应的JavaScript。

There is no point in building an Ajax handler from scratch when this problem has already be solved in various libraries just as jQuery and Prototype.

当这个问题已经在各种库中解决时,就像jQuery和Prototype一样,从头开始构建Ajax处理程序毫无意义。

#2


1  

In HTML inserted by Javascript does not execute automatically (at least in IE for sure). The only solution to this is to gather each of the script blocks in the loaded HTML and evaluate them each.

在Javascript插入的HTML中不会自动执行(至少在IE中是肯定的)。解决此问题的唯一方法是在加载的HTML中收集每个脚本块并对每个脚本块进行评估。

EDIT

编辑

I am using YUI here... the Dom class can collect all script tags from within the given block.

我在这里使用YUI ...... Dom类可以从给定的块中收集所有脚本标签。

var domElement = document.getElementById("Alex");
var scriptBlocks = YAHOO.util.Dom.getElementsBy(function() {return true;},'script',domElement);
for (var i = 0 ; i < scriptBlocks.length ; ++i){
    eval(scriptBlocks[i].innerHTML);
}

Simple as that. Also becareful about Internet Explorer... if you load in HTML using ajax, and it comes back with the script block as one of the first elements, it will, for some odd reason, ignore the script block and not include it in the response. To fix it, put a div above the script block with text in it with a style attribute of display:none;

就那么简单。同样考虑到Internet Explorer ...如果你使用ajax加载HTML,并且它以脚本块作为第一个元素之一返回,由于某些奇怪的原因,它将忽略脚本块而不包括在响应中。要修复它,请在脚本块上方放置一个div,其中包含一个样式属性为display:none;

If this is the HTML returned to IE, it will not include the script block in the response

如果这是返回给IE的HTML,则它不会在响应中包含脚本块

<div>
   <script type="text/javascript">
     /* Some javascript */
   </script>
</div>

This will fix the issue

这将解决问题

<div style="display:none;">some text</div>
<div>
   <script type="text/javascript">
     /* Some javascript */
   </script>
</div>

Very weird, but thats how IE rolls.

非常奇怪,但那就是IE如何滚动。

#3


0  

Use an absolute URI instead of a relative URL.

使用绝对URI而不是相对URL。

#4


0  

Adding a <script> element into a document via innerHTML doesn't(*) execute its contents as a script.

通过innerHTML将

You're also trying to insert the entire HTML document, including <html>, <head> and <body> inside a <div>, which is quite invalid.

您还试图在

中插入整个HTML文档,包括,和,这是非常无效的。

If you need to return both HTML and some script to execute, better to return a JSON object, eg.:

如果你需要返回HTML和一些脚本来执行,最好返回一个JSON对象,例如:

{
    "html": "<div id="Mathew"></div>",
    "js": "document.getElementById(\"Mathew\").innerHTML='ajax is working';"
}

then parse the JSON object, set the innerHTML to obj.html and eval the js. (Though it's generally questionable to be returning and executing arbitrary, scripts, there can sometimes be a use for it.)

然后解析JSON对象,将innerHTML设置为obj.html并评估js。 (虽然返回并执行任意脚本通常是有问题的,但有时可以使用它。)

(*: Well, doesn't generally. Exactly when a <script> element's contents get executed is browser-dependent. For example Firefox executes script when you append/insert a DOM HTMLScriptElement or ancestor into an element that is part of the document, whereas IE executes it when you insert the element into any parent for the first time, whether inside the document or not. In general, avoid inserting JavaScript-in-HTML content into HTML.)

(*:嗯,通常不会。确切地说,

#5


0  

Your methodology is slightly amiss. Typically, AJAX is used to send and receive data in non-HTML formats, such as XML, JSON, or sometimes even CSV (however, HTML is sometimes returned to the client, but usually as pieces of a page, not entire pages as in your example).

你的方法略有不妥。通常,AJAX用于以非HTML格式发送和接收数据,例如XML,JSON,有时甚至是CSV(但是,HTML有时会返回到客户端,但通常作为页面的一部分,而不是整个页面,如你的例子)。

Logic is rarely transmitted and is usually maintained on the respective sides of the transmission. In other words, the client/request side has all of its own logic and already knows what to do with the data returned from the server/response side (which also doesn't accept or require any logic generated from the client side). Further, the use of eval, which is usually necessary to consistently execute the logic found in the response, is generally frowned upon and considered a bad practice, thus the saying, "eval is evil."

逻辑很少传输,通常保持在传输的各个侧面。换句话说,客户端/请求方具有其自己的所有逻辑,并且已经知道如何处理从服务器/响应方返回的数据(其也不接受或要求从客户端生成的任何逻辑)。此外,使用eval(通常是必须始终如一地执行响应中的逻辑)通常不赞成并被认为是一种不好的做法,因此称之为“eval是邪恶的”。

In some cases, it may be necessary, advantageous or just plain easier to receive logic as part of the response from the server. In these situations however, it is still considered a best practice to separate your data from your logic.

在某些情况下,作为来自服务器的响应的一部分,接收逻辑可能是必要的,有利的或更简单的。但是,在这些情况下,将数据与逻辑分开仍然是最佳做法。

All that to nicely say that you're doing it wrong. I encourage you to read up on how AJAX works and how best to use it: w3schools walk-through, Mozilla MDC intro, AJAX and XML processing, updating a page (similar to what I think you're trying to do), and finally, AJAX API docs for jQuery, Prototype and Dojo.

所有这些都很好地说你做错了。我鼓励您阅读AJAX如何工作以及如何最好地使用它:w3schools演练,Mozilla MDC简介,AJAX和XML处理,更新页面(类似于我认为你想要做的),最后,jQuery,Prototype和Dojo的AJAX API文档。

#6


0  

Prototype has a nice way of handling this. See their stripScripts, extractStrips, and evalScripts methods on the String object. If you just strip the scripts, put your text into a div and then evalScripts, that'll work across all brwosers so scripts get executed exactly once.

Prototype有一个很好的处理方式。在String对象上查看stripScripts,extractStrips和evalScripts方法。如果您只是删除脚本,将文本放入div,然后再放入evalScripts,这将适用于所有brwosers,因此脚本只执行一次。