我正在尝试创建一个网页,使用jQuery显示相当于输入字符串的语法。代码适用于较小的字符串。但是,如果我在这里使用诸如PHP代码之类的长字符串,则完整字符串不会突出显示语法并显示:http://scriptings.tk/paste/515433887297e。不完整的字符串(字符串的一部分)是语法突出显示。当我使用ajax而不是jQuery尝试它时,出现了同样的问题。我不知道我做错了什么。这是代码。
index.html:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script type="text/javascript" src="preview.js"></script>
<meta http-equiv="content-type" content="text/html; charset=windows-1250">
<title>Test Code Preview</title>
</head>
<body>
<textarea id="pastecode" rows="20" cols="50" name="pastecode"></textarea>
<br /><input type="text" id="language" name="language"/>
<br /><input type="button" onclick="process()" value="Preview"/>
<br />
<div id="previewcode"></div>
</body>
</html>
preview.php:
<?php
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';
echo '<response>';
$code=$_POST['pastecode'];
$language=$_POST['language'];
include("geshi/geshi.php");
$path = '';
$geshi = new GeSHi($code, $language, $path);
$geshi->set_overall_style('background-color: #ffffee;', true);
$out = $geshi->parse_code();
echo htmlentities($out);
echo '</response>';
?>
preview.js:
function process(){
$.ajax({
url: "preview.php",
type: "post",
data: {
pastecode: $("pastecode").val(),
language: $("language").val()
},
success: function(data){
$("#previewcode").text(data);
//Or handle data with jQuery.parseXML()
},
error:function(jqXHR, textStatus, errorThrown){
alert("The following error occured: " + textStatus + " " + errorThrown);
}
});
}