Ajax对从JavaScript调用的php函数中的echo语句做什么? [英] What does ajax do with the echo statements in a php function called from javascript

查看:69
本文介绍了Ajax对从JavaScript调用的php函数中的echo语句做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这样的html页面末尾有一个脚本。

I have a script called at the end of an html page like this.

<body>

<script type="text/javascript">

 $(document).ready(function() {
               //define php info and make ajax call to recreate divs from XML data
               $.ajax({
                   url: "get_nodes.php",
                   type: "POST",
                   data: { },
                   cache: false,
                   success: function (response) {

                       if (response != '') 
                       {
                          alert(response);

                       }
                   }
               });
 });

</script>

</body>

php脚本具有可编写javascript的echo语句。 echo语句未打印在文档中。他们被送到某个地方,我不知道在哪里。如果我用alert函数查看从ajax调用到php的响应,则所有回显语句都会打印在警报框中,因为我希望它们会打印在文档本身中。

The php script has echo statements that write javascript. The echo statements are not printed in the document. They are being sent somewhere, I don't know where. If I look at the response from the ajax call to php with the alert function, all the echo statements are printed in the alert box as I would expect them to be printed in the document itself.

我的问题是:


  • ajax对php echo语句有何作用?

  • 将它们打印在文档中的方式是什么?

这是通过ajax调用的PHP文件get_nodes.php。

Here is the PHP file get_nodes.php called through ajax.

<?php

function get_nodes() {
// load SimpleXML
$nodes = new SimpleXMLElement('communities.xml', null, true);

foreach($nodes as $node) // loop through 
{

        echo "\n<script type='text/javascript'>\n";

        echo "  var newdiv = document.createElement('div');\n";
        echo "  newdiv.id = '".$node['ID']."';\n";
        echo "  newdiv.className ='comdiv ui-widget-content';\n";
        echo "  newdiv.style.top = '".$node->TOP."'\n";
        echo "  newdiv.style.left = '".$node->LEFT."';\n";
        echo "  newdiv.style.width = '".$node->WIDTH."';\n";
        echo "  newdiv.style.height = '".$node->HEIGHT."';\n";
        echo "  $( '#page' ).append(newdiv);\n";
        echo "  var heading = document.createElement('p');\n";
        echo "  heading.innerHTML = '".$node->NAME."';\n";
        echo "  heading.className ='comhdr editableText ui-widget-header';\n";
        echo "  $('#".$node['ID']."').append(heading);\n";
        echo "  $('#".$node['ID']."').resizable();";
        echo "  $('#".$node['ID']."').draggable();";
        echo "  $('#".$node['ID']."').draggable('option', 'handle', '.comhdr');";

        printf('$("#%s").append("<a href=\\"#\\" onClick=\\"delete_div(\'%s\');\\">Delete</a>&nbsp;&nbsp;");', $node

['ID'], $node['ID']);

        printf('$("#%s").append("<a href=\\"#\\" onClick=\\"add_url(\'%s\');\\">Add URL</a>&nbsp;&nbsp;");', $node

['ID'], $node['ID']);

        echo "\n</script>\n"; 
}

   return;
}

echo get_nodes();

?>


推荐答案

隐身指出,您的ajax调用并不重要有关您的PHP函数正在执行的操作-在这种情况下,最重要的是从服务器返回的内容,而不是内容的创建方式。

As Incognito pointed out, your ajax call does not care about what your PHP function is doing - all that matters in this case is the content returned from the server, not how the content is created.

尝试添加<$ c $ ajax调用中的$ c> dataType 选项可指定脚本的响应类型,并使用追加 appendTo 函数将脚本添加到文档主体:

Try adding the dataType option to your ajax call to specify a response type of script, and use the append or appendTo function to add the script to the document body:

$.ajax({
    url: "get_nodes.php",
    type: "POST",
    data: { },
    dataType: 'script',
    cache: false,
    success: function (response) {
        if (response != '') 
        {
            $(document.body).append(response);
        }
    }
});

另外,一个建议:在服务器端函数中,为什么不回显HTML标记而不是javascript创建标记的功能?

Also, a suggestion: In your server-side function, why not echo HTML markup instead of a javascript function which creates markup?

这篇关于Ajax对从JavaScript调用的php函数中的echo语句做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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