从Ajax响应(PHP)执行JavaScript函数返回 [英] Executing a javascript function returned from AJAX response (PHP)

查看:99
本文介绍了从Ajax响应(PHP)执行JavaScript函数返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图加载javascript函数一次Ajax已经通过PHP返回的HTML code。这需要我来呼应JavaScript中的Ajax响应。

I am trying to load a javascript function once Ajax has returned the HTML code through PHP. This requires me to echo the javascript in the ajax response.

换句话说,我想添加这个code(放置脚本标签之间)在PHP Ajax响应..希望它执行

In other words i am trying to add this code (placed between script tags) in the PHP Ajax response.. hoping that it executes

$('#green').smartpaginator({ Some code... });

这是我读到目前为止,浏览器已经读完了Javascript和不执行此。有没有办法做到这一点....?

From what I have read so far the browser has done reading the Javascript and will not execute this. Is there a way to do this.... ?

推荐答案

您必须评估该code这样

You have to Evaluate that code like this

eval("("+response+")");

如果你的回答包含HTML和JavaScript code你必须做这样的

If your response contains both html and javascript code you have to do like this

 $.ajax({
            url: "/snippets/js-in-ajax-response.html",
            context: document.body,
            success: function(responseText) {
                $("#response-div").html(responseText);
                $("#response-div").find("script").each(function(i) {
                    eval($(this).text());
                });
            }
        });

这篇关于从Ajax响应(PHP)执行JavaScript函数返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆