gpt4 book ai didi

javascript - 如何执行从ajax响应返回的javascript函数

转载 作者:行者123 更新时间:2023-11-30 16:05:40 25 4
gpt4 key购买 nike

我有一个函数,我想在 ajax 发出成功请求时执行,但它不会在这里执行我的 jquery 部分

$.ajax({  
type: "POST",
url: "https://www.example.com/create_chat.php",
data: dataString,
beforeSend: function()
{
$("#loading_indicator").show();
},
success: function(response)
{
$(".example").html(response);

}
});

这是来自 php 文件的响应

<script>start_chat(); alert("testing");</script>

我也试过添加这个

$(".example").find("script").each(function(i) {
eval($(this).text());
});

但没有任何作用

最佳答案

您对 create_chat 的回复可能表明将使用哪个功能。例如

$.ajax({  
type: "POST",
url: "https://www.example.com/create_chat.php",
data: {
dataString: dataString
},
beforeSend: function()
{
$("#loading_indicator").show();
},
success: function(response) // response could be 1,2,3,4.. etc
{
if(response==1) {
start_chat();
}
if(response==2) {
stop_chat();
}
if(response==3) {
change_chat_room();
}
...
$(".example").html(response);

}
});

关于javascript - 如何执行从ajax响应返回的javascript函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37102659/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com