Pure Javascript data Post
I need to post/send som开发者_如何学Pythone XML from one web page to my server. How could I do this?
And, yes, I do need to do this using nothing but pure Javascript.
Any suggestions?
For example
var xhr = getXMLHttpRequest();
function getXMLHttpRequest() {
var activeXVersions = ["Msxml2.DOMDocument.6.0","Msxml2.DOMDocument.3.0","Msxml2.XMLHTTP","Microsoft.XMLHTTP"];
try {
return new XMLHttpRequest;
} catch (e) {
for (var i=0; i < activeXVersions.length; i++) {
try {
return new ActiveXObject(activeXVersions[i]);
} catch (e) {}
}
}
return null;
}
function callAjax(url) {
xhr.open(“POST”, url, true);
var xmlContents = document.getElementById(‘xml′).value; // xml contents
xhr.onreadystatechange = handleAjaxResponse;
xhr.setRequestHeader(“Content-type”, “application/x-www-form-urlencoded”);
xhr.send (‘xml=’ + xmlContents );
}
function handleAjaxResponse() {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
alert( xhr.responseXML);
} else {
alert (‘An error occurred: ‘ + myRequest.statusText);
}
}
}
You can do this 1 of two ways.
Use XHR
Use an iframe
The first option is the modern way, and the preferred way these days.
My suggestion is to use a library like jQuery, which will make this a trivial matter using $.post
.
Here is what I believe to be a good tutorial for learning AJAX link
(Note: AJAX means Asynchronous JavaScript and XML and is pure Javascript.)
精彩评论