【发布时间】:2015-01-02 15:33:27
【问题描述】:
我有一个包含语言文件“lang.php”、“index.php”和 ajax 调用文件“ajax.php”的应用程序。 Lang 文件包含我在“index.php”和“ajax.php”中包含的 php 和 javascript 变量,因此我可以通过翻译“lang.php”文件来更改语言。
问题是当我在“ajax.php”中包含“lang.php”时,因为它在<script></script> 标签中包含javascript 变量,而ajax 将其解释为输出。当我删除<script> 标签并且“lang.php”仅包含 php 变量时,它可以正常工作。这是我的文件的内容。如何在不删除 javascript 变量的情况下执行此操作(因为我需要每种语言一个语言文件)。
index.php
include "lang.php";
function test()
{
$.ajax({
url: 'ajax.php',
type: 'POST',
success: function(data) {
//manipulating returned json;
},
error: function() {
alert(errorMessage);
}
});
};
<input type="button" onclick="test();">
lang.php
<script>
var errorMessage = "Error during ajax call";
</script>
<?php
$var1 = "Welcome";
$var2 = "Header";
?>
ajax.php
include "lang.php";
$array = array($var1, $var2);
echo json_encode($array);
die();
感谢您的帮助。
【问题讨论】:
-
我对这里的问题有点困惑。 什么 你想要ajax.php 输出吗?附言在 PHP 文件中,如果您在
<?php ?>标签的之外,则内容将被回显。另外,您的 index.php 中是否有<script>标签? -
我已经编辑了问题,很抱歉造成混淆。 Ajax.php 返回 json 对象,在 index.php 中我需要在 ajax 调用的成功部分操作该对象。是的,我在 index.php 中有脚本标签。
-
@user2204356 我添加了一个带有其他选项的 aswer,检查一下。
标签: javascript php ajax