微软的JavaScript,Post的实例(XMLHTTP)

下面这个functioon是我分析MSN的一个网址截取的,对使用javascript Post数据的时候有用,以前我都是用笨办法在页面上放个隐藏的form然后submit,现在可以直接用使用post了,^_^!

 

 

function _sd_Post(Url, Args)
{
 
var xmlhttp;
 
var error;
 eval('
try {xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {xmlhttp = null;error=e;}');
 
if(null != xmlhttp)
 {
 xmlhttp.Open(
"POST", Url, false);
 xmlhttp.setRequestHeader(
"Content-Type""application/x-www-form-urlencoded");
 xmlhttp.Send(Args);
 strText 
= xmlhttp.responseText;
 
// if our response starts with http than redirect there
 if ( strText != null && strText.toLowerCase().indexOf("http"== 0 )
 {
 location.href 
= strText;
 }
 }
 
else
 {
 location.href 
= c_errorUrl;
 }
 
return strText;
}
function GetActionUrl(listKey)
{
 
return document.getElementById("ActionUrl:" + listKey).url
}

 

 

靠!下面是从Google搜索到的更有用的,哈哈!

http://www.experts-exchange.com/Web/Web_Languages/Q_21132871.html

 

Solution Title: Microsoft.XMLHTTP sending a file 
asked by mikevandike on 
09/15/2004 04:21PM PDT  
 
 


Hi I am currently re
-developing a web based front end for an estate agent to manage their propertys. I have decided to use a Microsoft.XMLHTTP object to communicate between the client and server. My problem is got to do with sending a file with this. The client is composed of the usual GUI components which the data can be easily retrieved from for sending to the server but it also contains a file element for selecting a picture of the property to be uploaded. How do I get the data from this so that it can be uploaded to the server using the Microsoft.XMLHTTP object. I have found a bit of code on the net but it is no use to me because it would involve changing the security in "Internet Options" which I have no control over.

<HTML>
<HEAD><TITLE>File Send</TITLE></HEAD>
<BODY>
   
<INPUT id=btn_send name="btn_send" type=button value="FILE SEND">
   
<DIV id=div_message>Ready</DIV>
</BODY>
</HTML>

<SCRIPT LANGUAGE=JavaScript>

// files upload function
function btn_send.onclick()
{
   
// create ADO-stream Object
   var ado_stream = new ActiveXObject("ADODB.Stream");

   
// create XML document with default header and primary node
   var xml_dom = new ActiveXObject("MSXML2.DOMDocument");
   xml_dom.loadXML('
<?xml version="1.0" ?> <root/>');
   
// specify namespaces datatypes
   xml_dom.documentElement.setAttribute("xmlns:dt""urn:schemas-microsoft-com:datatypes");

   
// create a new node and set binary content
   var l_node1 = xml_dom.createElement("file1");
   l_node1.dataType 
= "bin.base64";
   
// open stream object and read source file
   ado_stream.Type = 1;  // 1=adTypeBinary 
   ado_stream.Open(); 
   ado_stream.LoadFromFile(
"c:\\sold.gif");
   
// store file content into XML node
   l_node1.nodeTypedValue = ado_stream.Read(-1); // -1=adReadAll
   ado_stream.Close();
   xml_dom.documentElement.appendChild(l_node1);

   
// we can create more XML nodes for multiple file upload

   
// send XML documento to Web server
   var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
   xmlhttp.open(
"POST","http://www.google.com",false);
   xmlhttp.send(xml_dom);
   
// show server message in message-area
   div_message.innerHTML = xmlhttp.ResponseText;
}
</SCRIPT>


thanks
John. 

 

其他资源......

 

 

The HttpRequest object provides client-side communication with a server.


Examples

readyState
How to return the state of the document. This property changes as the document is being loaded.

responseText
How to return the request as a string.

status
How to return the status of the operation, as a code.

statusText
How to return the status of the operation, as a string.


The HttpRequest object

With the httpRequest object you can send a request from the client to the server.

If you are using JavaScript in IE 5.0, you can create the HttpRequest object with the following code:

 

var xmlHTTP = new ActiveXObject("Microsoft.XMLHTTP")

 

If you are using VBScript you create the httpRequest object with the following code:

 

set xmlHTTP = CreateObject("Microsoft.XMLHTTP")

 

The httpRequest object is not a part of the W3C DOM standard.


Get XML

How to get an xml file from the server using the httpRequest object (works only in IE):

 

var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")
xmlHttp.open("GET", "note.xml", false)
xmlHttp.send()
xmlDoc=xmlHttp.responseText

 

Try it Yourself

Netscape compatible code:

 

xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", "note.xml", false);
xmlHttp.send(null);
xmlDoc = xmlHttp.responseText;

 

Try it Yourself


Send XML

You can also send an xml document to an ASP page on the server, analyze the request, and send back the result.

 

var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")
xmlHttp.open("POST", "demo_dom_http.asp", false)
xmlHttp.send(xmlDoc)
document.write(xmlHttp.responseText)

 

The ASP page, written in VBScript:

 

set xmldoc = Server.CreateObject("Microsoft.XMLDOM")
xmldoc.async=false
xmldoc.load(request)

for each x in xmldoc.documentElement.childNodes
   if x.NodeName = "to" then name=x.text
next

response.write(name)

 

You send the result back to the client using the response.write property.

Try it Yourself


Important Note

At the moment, the Microsoft XMLHTTP object can only be run in the BROWSER.

SERVER code that attempts to use the XMLHTTP to communicate with other Web servers, may function incorrectly or perform poorly.

This is a bug in the HTTPRequest object. For more information read Microsoft's Knowledge Base article Q237906.

The rumor is that Microsoft will have this bug fixed in an upcoming release of the XML Library. In the meantime, you may have to use a commercially available ASPHTTP component.


The httpRequest Properties

 

Property Description
readyState Returns the state of the document
responseBody Returns the response as an array of unsigned bytes
responseStream Returns the response as an IStream
responseText Returns the response as a string
responseXML Returns the response as an xml document
status Returns the status code as a number
statusText Returns the status as a string

 

The httpRequest Methods

 

Property Description
abort() Cancel the current http request
getAllResponseHeaders() Returns the value of the http headers
getResponseHeader(headerName) Returns the value of one specified http header
open(method, url, async, userid, password) Opens http request, and specifies the information
send() Send the http request to the server
setRequestHeader(headerName,headerValue) Specifies the name of a http header

 

posted on 2005-04-24 00:44  dragonpro  阅读(8012)  评论(1编辑  收藏  举报

Free Web Counter