从 AJAX 执行 Javascript 函数 - HTML 响应

发布于 2024-08-02 15:46:17 字数 1700 浏览 6 评论 0原文

我有一个显示评论的网页,用户可以单击“举报”链接来报告不当评论。

当他们单击 Flag 链接时,我使用 AJAX 和 innerHTML 在评论下方显示一个下拉框,其中包含原因代码,例如垃圾邮件、攻击性内容、与主题无关等,以及取消按钮。

如果用户单击“提交”,我想使用另一个 AJAX 请求将他们的响应发送到 PHP 文件,其中数据库被更新,并且他们最终会收到“谢谢”(无需重新加载页面)。我本质上希望使用另一个 AJAX 请求将显示下拉框的 DIV 替换为“谢谢”。

这就是问题所在。我似乎无法从第一个 AJAX 请求的 HTML 响应中执行 AJAX 请求。 JavaScript 函数失败——即使是简单的 Alert('hello world') 也不起作用。我尝试将 JavaScript 函数放置在调用第一个 AJAX 请求的主页中,并将其放置在显示为第一个 AJAX 请求的 HTML 响应的 PHP 文件中,但我没有任何运气 - 这些函数只是执行当他们被调用时不运行。

如果我从外部加载 PHP 文件,一切都会正常,所以我知道 JavaScript 是正确的。当我将 PHP 文件加载到 HTML 响应 DIV 中然后从那里调用 JavaScript 时,它不起作用。

总而言之,如何从 AJAX 请求的 HTML 响应中执行 JavaScript 函数?

编辑:这是我想要做的示例:

这是当用户单击 Flag 链接时填充 DIV 的 AJAX 部分:

xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4)
{
     document.getElementById(whichdiv).innerHTML=xmlhttp.responseText;
}
};

xmlhttp.responseText 的值来自此外部文件:

<input type="hidden"/>
<script type="text/javascript" language="javascript">
function displayalert()
{
    alert ('Hello World!');
}
</script>

<form name="myform" id="myform">
<input type="text" name="myfield" value="teststring"/><br/>
<input type="button" name="button" value="Submit" 
   onclick="displayalert();"/>
</form>

注意: <上面的 input type="hidden"/> 来自我在 http://msdn.microsoft.com/en-us/library/ms533897%28VS.85%29.aspx

当用户单击按钮时,javascript displayalert() 函数不会运行。警报框永远不会弹出。如果我从外部加载文件而不是使用innerHTML 调用它,则脚本可以正常工作。

xmlhttp.responseText 可以包含 JavaScript 代码吗?

I have a web page that displays comments, and users can click a 'Flag' link to report an inappropriate comment.

When they click the Flag link, I use AJAX and innerHTML to display a dropdown box underneath the comment with a reason code, e.g. Spam, Offensive, Unrelated to topic, etc., as well as a Cancel button.

If the user clicks Submit, I want to use another AJAX request to send their response to a PHP file, where the database is updated, and they receive a "Thank you" on their end (without reloading the page). I essentially want the DIV that displays the dropdown box to be replaced with "Thank you" using another AJAX request.

That's where the problem is. It seems that I cannot execute an AJAX request from within the HTML response from the first AJAX request. The JavaScript functions fail -- even a simple Alert('hello world') doesn't work. I tried placing the JavaScript functions in the main page that calls the first AJAX request, as well placing it in the PHP file that displays as the HTML response from the first AJAX request, but I did not have any luck -- the functions just do not run when they are called.

Everything works fine if I load the PHP file externally, so I know the JavaScript is correct. It just doesn't work when I load the PHP file into the HTML response DIV and then call the JavaScript from there.

So to sum everything up, how do you execute JavaScript functions from the HTML response of an AJAX request?

EDIT: here is a sample of what I want to do:

This is the AJAX part that populates the DIV when the person clicks the Flag link:

xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4)
{
     document.getElementById(whichdiv).innerHTML=xmlhttp.responseText;
}
};

The value of xmlhttp.responseText comes from this external file:

<input type="hidden"/>
<script type="text/javascript" language="javascript">
function displayalert()
{
    alert ('Hello World!');
}
</script>

<form name="myform" id="myform">
<input type="text" name="myfield" value="teststring"/><br/>
<input type="button" name="button" value="Submit" 
   onclick="displayalert();"/>
</form>

Note: the <input type="hidden"/> above comes from a suggestion I found off of http://msdn.microsoft.com/en-us/library/ms533897%28VS.85%29.aspx.

When the user clicks the button, the javascript displayalert() function doesn't run. The alert box never pops up. If I load the file externally instead of calling it with innerHTML, the script works fine.

Can the xmlhttp.responseText contain JavaScript code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼泪也成诗 2024-08-09 15:46:17

取决于浏览器:
IE 不支持使用 ajax 加载的 html 上的 scriptEval,这意味着如果 html 中有脚本块,则不会调用它们。

Firefox 支持脚本 eval。

我通常做的是将一些 json 放入输入中,然后检查浏览器是否支持 scriptEval,如果不支持,则拉取 json,对其进行评估,然后调用一些传递 json 的方法。

如果浏览器支持 scriptEval,我还包含一个脚本块,其中包含对 json 中相同方法的调用。

您可能还想阅读以下内容:
http://webreflection.blogspot.com/2007/ 08/global-scope-evaluation-and-dom.html

depends on the browser:
IE doesnt support scriptEval on html that is loaded with ajax, which means that if you have script blocks in your html, they wont be called.

Firefox supports script eval.

What i usually do is shove some json into an input, then check if the browser supports scriptEval, if it doesnt, pull the json, eval it, and call some method passing json.

if the browser supports scriptEval, i also include a script block that contains a call to the same method with the json.

you may also want to read this:
http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文