开发者

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.

  1. Use XHR

  2. 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.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜