JavaScript (带有Ajax) 从PHP中输出缓冲区

4

我现在有一个来自我的PHP脚本的JSON格式文件。

下一步是编写JavaScript脚本,以检索此数据进行排序、过滤和显示。

我有一个工作的Ajax脚本,用于拉回数据测试正常,但我需要将其个性化到个人。

在PHP中,我有一个名为MID(会员ID)的Session变量。

我正在尝试使用PHP使用MID作为变量构建具有唯一URL的JavaScript。

以下所有内容似乎都有效,除了在JavaScript文本中替换midValue变量与外部PHP脚本中的MID变量。

到目前为止,代码看起来像这样...



    // This is a PHP file
    // Setup PHP Output Buffering to change the MID value
    session_start();
    $MID = $_SESSION['MID'];

    function callback($buffer)
    {
      return (str_replace("midValue", $MID, $buffer));
    }

    ob_start("callback");

/*

Some bits I can't show as I haven't figured out the correct Stackoverflow tags (!) ...

 - Add the usual HTML tags such as `HTML, HEAD, TITLE, BODY, SCRIPT` etc
 - Include a DIV with an ID of **json**, this will be replaced by the JSON output it
   self.
 - Enclose the params variable with the `CDATA` tags to maintain the ampersand.

*/
    params = "url=server.com/content.php?action=json&MID=" + midValue

    request = new ajaxRequest()
    request.open("POST", "getcontent.php", true)
    request.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
    request.setRequestHeader("Content-length", params.length)
    request.setRequestHeader("Connection", "close")

    request.onreadystatechange = function()
    {
        if (this.readyState == 4)
        {
            if(this.status == 200)
            {
                if(this.responseText != null)
                {
                    document.getElementById('json').innerHTML = this.responseText
                }
                else alert("Ajax Error: No data recieved")
            }
            else alert("Ajax Error: " + this.statusText)
        }
    }

    request.send(params)

    function ajaxRequest()
    {
        try
        {
            var request = new XMLHttpRequest()
        }
        catch(e1)
        {
            try
            {
                request = new ActiveXObject("Msxml2.XMLHTTP")
            }
            catch(e2)
            {
                try
                {
                    request = new ActiveXObject("Microsoft.XMLHTTP")
                }
            catch(e3)
                {
                    request = false
                }
            }
    }
    return request
   }

/*
Add the closing `SCRIPT, BODY and HTML` tags here.
*/

    ob_end_flush();

而 getcontent.php 文件看起来像这样...

       if(isset($_POST['url'])) {
            echo file_get_contents("http://" . SanitizeString($_POST['url']));
       }

       function SanitizeString($var) {
           $var = strip_tags($var);
           $var = htmlentities($var);
           return stripslashes($var);
       }
1个回答

1

我认为像这样更简单的东西应该对你有用。

<?php

session_start();
$MID = $_SESSION['MID'];
?>

params = "url=server.com/content.php?action=json&MID=<?php echo $MID ?>"

request = new ajaxRequest()
request.open("POST", "getcontent.php", true)
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
request.setRequestHeader("Content-length", params.length)
request.setRequestHeader("Connection", "close")

... // rest of javascript

<?php include 'footer.php'; // include footer code here ?>

使用这种方法,您只需在PHP之外输出JavaScript和HTML,因此无需在标签中使用它。然后,您可以轻松地输出变量或在需要时包含页眉和页脚。

谢谢drew010,这确实解决了最初的问题。但是,就像通常情况下一样(尤其是对于像我这样经验不足的人来说),我又遇到了另一个问题!在url中的和之后的所有内容(在params变量内)都被丢弃了。我以为用CDATA标签解决了这个问题。看来我需要更多地研究url编码。再次感谢。 - Peter Gross
我修复了这一部分。我不得不像你建议的那样使用myUrl = "server.com/content.php?action=json&mid=<?php echo $mid ?>",然后params = "url=" + encodeURIComponent(myUrl)。 - Peter Gross

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接