简体   繁体   English

IE为什么无法识别AJAX调用中返回的JavaScript代码?

[英]Why won't IE recognize javascript code returned in AJAX call?

I have a (hopefully) simple question. 我有一个(希望)简单的问题。

I'm using ajax to update a DIV on my current page. 我正在使用ajax更新当前页面上的DIV。 Within what's being inserted into the div is a script block that defines a variable. 在div中插入的是定义变量的脚本块。 Within FF, I can access the variable, but IE doesn't see the var. 在FF中,我可以访问变量,但是IE看不到var。 Do I need to eval the returned code in some way for IE to recognize the variable? 我是否需要以某种方式评估返回的代码,以便IE识别变量?

As a simple example, if my ajax call returns this snippet of code and gets inserted into the div, I get an alert in FF but not in IE: 举一个简单的例子,如果我的ajax调用返回此代码段并插入到div中,则在FF中会收到警报,但在IE中不会:

<script language='JavaScript' type='text/JavaScript'> alert('Foo!'); <script language ='JavaScript'type ='text / JavaScript'> alert('Foo!'); </script> </ SCRIPT>

是的,常见的解决方法是使用getElementsByTagName()获取所有script元素,然后对它们进行eval()

You should return a JSON object as the result of the AJAX call and then use the json2.js Javascript stuff to evaluate it into a live object. 作为AJAX调用的结果,您应该返回一个JSON对象,然后使用json2.js Javascript内容将其评估为活动对象。

http://json.org/ has a link to JSON libraries for every language known to man, including Javascript. http://json.org/包含指向人类已知的每种语言(包括Javascript)的JSON库的链接。

The problem might be your JavaScript MIME type - you're using an unconventional type (type='text/JavaScript'): 问题可能出在您的JavaScript MIME类型上-您使用的是非常规类型(type ='text / JavaScript'):

<script language='JavaScript' type='text/JavaScript'> alert('Foo!'); </script>

You should try this instead: 您应该尝试以下方法:

<script type='text/javascript'> alert('Foo!'); </script>

I ran some tests recently and found that even modern browsers are pretty picky about this MIME type (skip to Additional Test #1), and will refuse to execute the JavaScript if it isn't set correctly: http://davidbcalhoun.com/2010/what-happens-when-we-serve-javascript-with-random-mime-types 我最近进行了一些测试,发现即使是现代浏览器也对这种MIME类型非常挑剔(跳至附加测试#1),如果设置不正确,则将拒绝执行JavaScript: http : //davidbcalhoun.com/ 2010 /乜发生-时,我们先得的JavaScript,与随机-MIME类型

If that's not the issue, try adding the script to the document Head: 如果这不是问题,请尝试将脚本添加到文档Head中:

document.getElementsByTagName('head')[0].appendChild(script);

This is what Google Analytics did until recently, so I would be surprised if it didn't work for you. 这是Google Analytics(分析)直到最近才做的事情,所以如果它对您不起作用,我会感到惊讶。 You shouldn't need to use eval(). 您不需要使用eval()。

According to this page this page , IE is picky with nodes that haven't yet loaded, but the Head is almost guaranteed to have loaded, which is why it shouldn't have the same issue. 根据此页面的本页 ,IE对尚未加载的节点很挑剔,但是几乎可以保证Head已加载,这就是为什么它不应该出现相同的问题。

为什么使用 AJAX 返回的 Javascript 代码未写入指定的<div> ,但它会替换整个页面?</div><div id="text_translate"><p> 我用 AJAX(使用 jQuery)请求一个包含 Javascript function 调用 Apache+PHP 服务器的标签。 这是 HTML 和 AJAX 代码:</p><pre> &lt;.DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1:1//EN" "http.//www.w3.org/TR/xhtml11/DTD/xhtml11:dtd"&gt; &lt;html xmlns="http.//www.w3:org/1999/xhtml"&gt; &lt;head&gt; &lt;script type="text/javascript" src="http.//localhost/LivingLab/javascript/jquery.min:js"&gt;&lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;a href="javascript. void(0)" onclick=" $:post( 'http.//localhost/test,php': // server target page {some_var, 'abc'}. // data to be sent via POST method function(data) { // JS callback function $('#container');html(data); // innerHTML of &lt;div id="container"&gt; will be replaced } ) "&gt;Click me!&lt;/a&gt; &lt;div id="container"&gt;&lt;/div&gt; &lt;/body&gt; &lt;/html&gt;</pre><p> AJAX 请求具有以下 PHP 代码的页面“http://localhost/test.php”:</p><pre> &lt;?php echo '&lt;script type="text/javascript"&gt; document.write("Javascript output"); &lt;/script&gt;'; ?&gt;</pre><p> 当我点击链接时,在 AJAX 请求之后,整个页面被“Javascript 输出”替换,而不是仅在 id 为“容器”的 div 标记内写入。</p><p> 如果 PHP 服务器脚本写入其他内容,而不是像这样的脚本标记:</p><pre> &lt;?php echo 'text'?&gt;</pre><p> AJAX 调用行为正常,通过将 div 替换为 id“容器”而不是整个页面。</p><p> 我尝试使用 Wireshark 调查此页面传输的 HTTP 数据,它似乎不是 HTTP 问题,因为服务器响应正是它应该的样子:</p><pre> &lt;script type="text/javascript"&gt; document.write("Javascript output"); &lt;/script&gt;</pre><p> 在没有 AJAX 的情况下运行 Javascript 代码不会替换整个页面,仅当使用 AJAX 返回脚本标记时才会发生这种情况。 A 注意到 Firefox 3 和 5 以及 Google Chrome 中的相同行为。</p><p> 为什么会这样?</p></div> - Why a Javascript code returned with AJAX doesn't write in a specified <div>, but it replaces the whole page?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的 JavaScript 代码在 jquery ajax 调用之后不会运行 - My JavaScript code won't run after jquery ajax call Ajax 返回的 html 似乎无法识别适用于它的 javascript 函数 - Ajax returned html doesn't seem to recognize javascript functions that apply to it 为什么这个 ajax 调用不能在这个简单的 object 文字 ajax 框架中为 ZDE9B9ED78D7E2E1DCEEFFEE71 工作 - Why won't this ajax call work in this simple object literal ajax frame work for javascript IE问题:将不会执行作为AJAX内容的一部分加载的javascript - IE Problem: Won't execute javascript loaded as part of AJAX content 为什么使用 AJAX 返回的 Javascript 代码未写入指定的<div> ,但它会替换整个页面?</div><div id="text_translate"><p> 我用 AJAX(使用 jQuery)请求一个包含 Javascript function 调用 Apache+PHP 服务器的标签。 这是 HTML 和 AJAX 代码:</p><pre> &lt;.DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1:1//EN" "http.//www.w3.org/TR/xhtml11/DTD/xhtml11:dtd"&gt; &lt;html xmlns="http.//www.w3:org/1999/xhtml"&gt; &lt;head&gt; &lt;script type="text/javascript" src="http.//localhost/LivingLab/javascript/jquery.min:js"&gt;&lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;a href="javascript. void(0)" onclick=" $:post( 'http.//localhost/test,php': // server target page {some_var, 'abc'}. // data to be sent via POST method function(data) { // JS callback function $('#container');html(data); // innerHTML of &lt;div id="container"&gt; will be replaced } ) "&gt;Click me!&lt;/a&gt; &lt;div id="container"&gt;&lt;/div&gt; &lt;/body&gt; &lt;/html&gt;</pre><p> AJAX 请求具有以下 PHP 代码的页面“http://localhost/test.php”:</p><pre> &lt;?php echo '&lt;script type="text/javascript"&gt; document.write("Javascript output"); &lt;/script&gt;'; ?&gt;</pre><p> 当我点击链接时,在 AJAX 请求之后,整个页面被“Javascript 输出”替换,而不是仅在 id 为“容器”的 div 标记内写入。</p><p> 如果 PHP 服务器脚本写入其他内容,而不是像这样的脚本标记:</p><pre> &lt;?php echo 'text'?&gt;</pre><p> AJAX 调用行为正常,通过将 div 替换为 id“容器”而不是整个页面。</p><p> 我尝试使用 Wireshark 调查此页面传输的 HTTP 数据,它似乎不是 HTTP 问题,因为服务器响应正是它应该的样子:</p><pre> &lt;script type="text/javascript"&gt; document.write("Javascript output"); &lt;/script&gt;</pre><p> 在没有 AJAX 的情况下运行 Javascript 代码不会替换整个页面,仅当使用 AJAX 返回脚本标记时才会发生这种情况。 A 注意到 Firefox 3 和 5 以及 Google Chrome 中的相同行为。</p><p> 为什么会这样?</p></div> - Why a Javascript code returned with AJAX doesn't write in a specified <div>, but it replaces the whole page? 调用从ajax调用返回的javascript代码中的.trigger(“ click”) - calling .trigger(“click”) that is in javascript code returned from an ajax call 为什么不运行Javascript代码? - Why won't Javascript code run? 为什么这个 Javascript 代码不加 1? - Why won't this Javascript code increment by 1? 您如何获得 javascript function 以识别 ajax 请求返回的 html 代码 - How do you get a javascript function to recognize html code returned by an ajax request 为什么任何版本的IE都不会处理此Javascript? - Why won't any version of IE process this Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM