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

时间:2022-08-25 18:17:38

I have a function which i want to execute when ajax makes a successful request but it doesn't execute here my jquery part

我有一个函数,我想在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);

            }
        }); 

here is the response from php file

这是php文件的响应

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

i tried adding this also

我也尝试过添加它

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

but nothing works

但没有任何作用

3 个解决方案

#1


1  

Your response from create_chat could indicate which function will be used. For example

您对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);

            }
        }); 

#2


1  

While there are work arounds, you should never be executing the response of an Ajax call directly.

虽然有解决方法,但您永远不应该直接执行Ajax调用的响应。

"Scripts in the resulting document tree will not be executed, resources referenced will not be loaded and no associated XSLT will be applied."

“将不会执行生成的文档树中的脚本,不会加载引用的资源,也不会应用任何关联的XSLT。”

http://www.w3.org/TR/XMLHttpRequest/#document-response-entity-body

http://www.w3.org/TR/XMLHttpRequest/#document-response-entity-body

Best practice is to respond with data, typically in the form of JSON, JSONP or HTML.

最佳做法是使用数据进行响应,通常采用JSON,JSONP或HTML格式。

#3


0  

Try This,

尝试这个,

  $.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');
    }
  });

#1


1  

Your response from create_chat could indicate which function will be used. For example

您对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);

            }
        }); 

#2


1  

While there are work arounds, you should never be executing the response of an Ajax call directly.

虽然有解决方法,但您永远不应该直接执行Ajax调用的响应。

"Scripts in the resulting document tree will not be executed, resources referenced will not be loaded and no associated XSLT will be applied."

“将不会执行生成的文档树中的脚本,不会加载引用的资源,也不会应用任何关联的XSLT。”

http://www.w3.org/TR/XMLHttpRequest/#document-response-entity-body

http://www.w3.org/TR/XMLHttpRequest/#document-response-entity-body

Best practice is to respond with data, typically in the form of JSON, JSONP or HTML.

最佳做法是使用数据进行响应,通常采用JSON,JSONP或HTML格式。

#3


0  

Try This,

尝试这个,

  $.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');
    }
  });