微软的JavaScript,Post的实例(XMLHTTP)
2008-01-22 11:53
148 查看
下面这个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.
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.
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
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:
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
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 YourselfImportant 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 |
相关文章推荐
- 微软的JavaScript,Post的实例(XMLHTTP)
- 微软的JavaScript,Post的实例(XMLHTTP)
- 微软的JavaScript,Post的实例(XMLHTTP)
- javascript中ajax post实例详解
- javascript之post网站用get方式采集实例
- JavaScript原生xmlHttp与jquery的ajax方法json数据格式实例
- Ajax之get、post实例(最原始的XMLHttpServlet)
- JavaScript原生xmlHttp与jquery的ajax方法json数据格式实例
- Javascript使用post方法提交数据实例
- Javascript使用post方法提交数据实例
- javascript中checkbox使用方法实例演示
- JavaScript入门<3>JS外置对象:HTML DOM与Window子对象实例详解
- javascript HTMLEncode HTMLDecode的完整实例(兼容ie和火狐)
- javascript实现数字验证码的简单实例
- JavaScript黑洞数字之运算路线查找算法(递归算法)实例
- [JavaScript实例] 兼容IE和最新FF的复制粘贴代码
- JavaScript正则表达式实例详解
- JavaScript全局变量和局部变量实例
- Javascript查询DBpedia小应用实例学习
- javascript-第八节-DOM对象实例集合