dongtang1909 2013-07-03 10:17
浏览 60
已采纳

使用ajax无响应的代码预览

I am trying to do the following: When the user presses the preview button, the syntax highlighted code is shown using AJAX but it is not working. What am I doing wrong? Here are the three codes : 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 /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
         <div id="previewcode"></div>
  </body>
</html>

preview.js :

var xmlHttp= createXmlHttpRequestObject();

function createXmlHttpRequestObject(){
   var xmlHttp;

   if(window.ActiveXObject){
      try{
         xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
         }catch(e){
            xmlHttp =false;
            }
      }else{
         try{
            xmlHttp= new XMLHttpRequest();
            }catch(e){
               xmlHttp =false;
               }
         }
      if(!xmlHttp)
            alert("cant create that object hoss!");
      else
            return xmlHttp;
   }

function process(){   
    if(xmlHttp.readyState==0 || xmlHttp.readyState==4){
      code=encodeURIComponent(document.getElementById("pastecode").value);
      language=encodeURIComponent(document.getElementById("language").value);
      xmlHttp.open("POST", "preview.php", true);
      xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
      xmlHttp.onreadystatechange = handleServerResponse;
      xmlHttp.send("pastecode="+code+"&language="+language);
      }
   }


function handleServerResponse(){
   if(xmlHttp.readyState==4){
            if(xmlHttp.status==200){
               xmlResponse=xmlHttp.responseXML;
               xmlDocumentElement=xmlResponse.documentElement;
               code=xmlDocumentElement.firstChild.data;
               document.getElementById('previewcode').innerHTML=code;
         }
      }
   }

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>';
?>

What is happening is, I am filling up the textbox for pastecode and the textbox for language and clicking the button but there is no response. I am using Firefox.

  • 写回答

1条回答 默认 最新

  • dtkf64283 2013-07-03 10:25
    关注

    You should really be using a library like JQuery for your Ajax requests. That way, you won't have to worry about the discrepancies between different browsers and browser versions.

    Example of what preview.js could look like:

    function process(){
    
        $.ajax({
          url: "preview.php",
          type: "post",
          data: {
              pastecode: $("pastecode").val(),
              language: $("language").val()
          },
          success: function(data){
               $("#previewcode").html(data);
               //Or handle data with jQuery.parseXML()
          },
          error:function(jqXHR, textStatus, errorThrown){
              alert("The following error occured: " + textStatus + " " + errorThrown);
          }   
        }); 
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)