我有一个函数,我想在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());
});
但没有任何作用
答案 0 :(得分:1)
您对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);
}
});
答案 1 :(得分:1)
虽然有解决方法,但您永远不应该直接执行Ajax调用的响应。
“生成的文档树中的脚本不会被执行, 引用的资源将不会被加载,并且不会关联XSLT 应用“。
http://www.w3.org/TR/XMLHttpRequest/#document-response-entity-body
最佳做法是使用数据进行响应,通常采用JSON,JSONP或HTML格式。
答案 2 :(得分:0)
试试这个,
$.ajax({
type: "POST",
data: {
'dataString': dataString
},
url: 'https://www.example.com/create_chat.php',
success: function(data) {
start_chat();
alert('Success');
},
error: function(data) {
alert('failed');
}
});