来自PHP的JavaScript(使用Ajax)和输出缓冲
我现在有一个来自PHP脚本的JSON格式的工作文件 下一步是使用JavaScript脚本检索这些数据以进行排序、过滤和显示 我有一个可以正常工作的Ajax脚本,可以测试回拉数据,但是我需要针对个人对其进行个性化设置 在PHP中,我有一个名为MID(成员ID)的会话变量 我正在尝试使用PHP构建JavaScript,其中包含唯一的URL,MID作为变量 除了用外部PHP脚本中的MID变量替换JavaScript文本中的midValue变量外,以下所有操作似乎都能正常工作 到目前为止,代码看起来像这样来自PHP的JavaScript(使用Ajax)和输出缓冲,javascript,php,ajax,output-buffering,Javascript,Php,Ajax,Output Buffering,我现在有一个来自PHP脚本的JSON格式的工作文件 下一步是使用JavaScript脚本检索这些数据以进行排序、过滤和显示 我有一个可以正常工作的Ajax脚本,可以测试回拉数据,但是我需要针对个人对其进行个性化设置 在PHP中,我有一个名为MID(成员ID)的会话变量 我正在尝试使用PHP构建JavaScript,其中包含唯一的URL,MID作为变量 除了用外部PHP脚本中的MID变量替换JavaScript文本中的midValue变量外,以下所有操作似乎都能正常工作 到目前为止,代码看起来像这
// 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();
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);
}
getcontent.php文件如下所示
// 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();
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);
}
我认为像这样简单的事情对你来说应该很好
<?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 ?>
params=“url=server.com/content.php?action=json&MID=”
请求=新的ajaxRequest()
open(“POST”,“getcontent.php”,true)
request.setRequestHeader(“内容类型”,“应用程序/x-www-form-urlencoded”)
request.setRequestHeader(“内容长度”,参数长度)
setRequestHeader(“连接”、“关闭”)
... // javascript的其余部分
使用此方法,您只需在PHP之外输出javascript和html,因此不需要在标记中使用它。然后,您可以根据需要回显变量或包含页眉和页脚。谢谢drew010,这确实解决了最初的问题。但就像经常发生的那样(尤其是像我这样没有经验的黑客),我直接遇到了另一个!将删除url中的符号及其后面的所有内容(在params变量中)。我以为CDATA标签解决了这个问题。看起来我需要对url编码做更多的研究。再次感谢。我把这一点修好了。我不得不用。。。myUrl=“server.com/content.php?action=json&mid=”就像你建议的那样,然后。。params=“url=”+encodeURIComponent(myUrl)