Javascript - 如何捕获和回复帖子?

时间:2010-12-06 05:25:59

标签: javascript html post xmlhttprequest response

我正在开发一个应用程序以响应打击,但我遗漏了一些东西。我可以很好地创建响应XML文档,但接收端没有看到我的响应。我在网上发现了数百个关于后期处理的例子,但似乎无法弄清楚服务器应该做些什么来回复帖子。如果有人可以提供服务器端的示例,那将非常感激。

更新
这是我的代码。我确实用显示器替换了SEND以查看我想要发送的内容。我对此非常陌生,所以任何见解都会有所帮助。

<script language="javascript">



var xmlhttp;
var xmlrtn;
<!-- Begin
  Stamp = new Date();
  year = Stamp.getYear();
  month = Stamp.getMonth() + 1;
  if (month < 10) {month = "0" + month;}
  ddate = Stamp.getDate();
  if (ddate < 10) {ddate = "0" + ddate;}
  if (year < 2000) year = 1900 + year;
  //document.write(year + "-" + month + "-" + ddate);
  timestamp = year + "-" + month + "-" + ddate;
  var Hours;
  var Mins;
  var Time;
  Hours = Stamp.getHours();
  Mins = Stamp.getMinutes();
  if (Mins < 10) {
   Mins = "0" + Mins;
  } 
  Secs = Stamp.getSeconds();
  if (Secs < 10) {Secs = "0" + Secs;}
  //document.write('T' + Hours + ":" + Mins + ":" + Secs);
  timestamp = timestamp + 'T' + Hours + ":" + Mins + ":" + Secs;
 // End -->

xmlhttp=new ActiveXObject("Microsoft.XMLDOM");   

 var o="&lt;";
 var c="/" + "&gt;"; 
 var tc="&lt;" + "/";
 var e="&gt;"; 
 var params=new Array();
 var xmlrtn = '';



function loadXML(xmlFile)
 {
   xmlhttp.async="false";
   xmlhttp.onreadystatechange=verify;
   xmlhttp.load(xmlFile);
   xmlObj=xmlhttp.documentElement;
 }

function verify()
 {
   // 0 Object is not initialized
   // 1 Loading object is loading data
   // 2 Loaded object has loaded data
   // 3 Data from object can be worked with
   // 4 Object completely initialized
   if (xmlhttp.readyState != 4)
  {
        return false;
  }
 }


</script>
</head>
<body>



 <script language="javascript">


//Read in XML file
 loadXML('https://www.americantexchem.com:9443/storefrontContent/attach/sample.xml');

 //Assign Variables
 var xmlver='?xml version="1.0"?';
 var doctype='!DOCTYPE cXML SYSTEM "http://xml.cxml.org/schemas/cXML/1.1.007/cXML.dtd"';

 xmlLang=xmlObj.getAttribute("xml:lang");

 var cxmltag=xmlObj.tagName;
 var cxmlval=' version="' + xmlObj.getAttribute("version") + '" payloadID="' + xmlObj.getAttribute("payloadID") + '"   timestamp="' + timestamp + '"';

 var resptag='Response';
 var statustag='Status code="200" text="success" ';
 var poutsrtag='PunchOutSetupResponse';
 var startpgtag='StartPage';
 var urltag='URL';
 var urlval='https://www.americantexchem.com:9443/storefrontCommerce/jsp/wynlogin.jsp';
 var srcurlval = xmlObj.childNodes(1).childNodes(0).childNodes(2).childNodes(0).firstChild.text; //URL content

 // post


  params[0] = o + xmlver + e;
  params[1] = o + doctype + e;
  params[2] = o + cxmltag;
  params[3] = cxmlval + e;
  params[4] = o + resptag + e;
  params[5] = o + statustag + c;
  params[6] = o + poutsrtag + e;
  params[7] = o + startpgtag + e;
  params[8] = o + urltag + e;
  params[9] = urlval;
  params[10] = tc + urltag + e;
  params[11] = tc + startpgtag + e;
  params[12] = tc + poutsrtag + e;
  params[13] = tc + resptag + e;
  params[14] = tc + cxmltag + e;

  for (var i in params) { 
           if (params.hasOwnProperty(i)) { 
           // var input = document.createElement('input'); 
               //   input.type = 'hidden'; 
                 // input.name = i; 
           //input.value = params[i]; 
              //    form.appendChild(input);
              xmlrtn = xmlrtn + params[i];  
    } 
   } 

 // alert (xmlObj.xml);
 document.write (xmlrtn);



 /*
  var fso = new ActiveXObject("Scripting.FileSystemObject");
  fileBool = fso.FileExists("C:\\out.xml");
  if(fileBool)
  {
   //document.write("Test-fileBool");
   fso.DeleteFile("C:\\out.xml",true);
  }

  var fso, s; 
    fso = new ActiveXObject("Scripting.FileSystemObject"); 
    s = fso.OpenTextFile("C:\\out.xml" , 8, true); 


  s.writeline (o + xmlver + e);
  s.writeline (o + doctype + e);
  s.writeline (o + cxmltag);
  s.writeline (cxmlval + e);
  s.writeline (o + resptag + e);
  s.writeline (o + statustag + c);
  s.writeline (o + poutsrtag + e);
  s.writeline (o + startpgtag + e);
  s.writeline (o + urltag + e);
  s.writeline (urlval);
  s.writeline (tc + urltag + e);
  s.writeline (tc + startpgtag + e);
  s.writeline (tc + poutsrtag + e);
  s.writeline (tc + resptag + e);
  s.writeline (tc + cxmltag + e);

//  s.writeline (xmlObj.xml); // the whole source xml document

 s.Close(); 

*/
</script>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

这将取决于您的服务器端技术。以下是使用asp.net mvc的示例帖子。

http://blog.bobcravens.com/2009/11/ajax-calls-to-asp-net-mvc-action-methods-using-jquery/

希望这有助于您入门

鲍勃