Cross-Domain Commit

Source: Internet
Author: User

This paper implements common cross-domain submissions, not AJAX submissions;

One, HTML form:

<form id= "FM" name= "FM" action= "server.aspx" method= "post" target= "Ajaxiframe" >    <input type= "text" name= "Tit" value= "title"/>    <input type= "Submit" value= "Commit"/></form>

After submitting the form, the data transfer is as follows:

Second, only the submission can not meet our needs, but also must have the return value of the service side;

Here the value is timed with setinterval:

HTML form:

<form id= "FM" name= "FM" action= "server.aspx" method= "post" target= "Ajaxiframe" >    <input type= "text" name= "Tit" value= "title"/>    <input type= "Submit" value= "submit"/></form><iframe id= "Ajaxiframe" Name= " Ajaxiframe "width=" 0 "height=" 0 "></iframe>

JS Code

<script type= "Text/javascript" > (function (iframe) {var Timer = setinterval (function () {if (                Iframe.contentwindow) {clearinterval (Timer); Bind event if (iframe.attachevent) {//ie iframe.attachevent (' onload ', Fu                        Nction () {var result = Iframe.contentWindow.document.documentElement.innerText;                        if (result.length>0) {alert (' Submit succeeded! (' + result + ') ');                }                    }); } else {Iframe.addeventlistener (' Load ', function () {var result = Iframe.conte                        NtWindow.document.documentElement.textContent;                        if (Result.length > 0) {alert (' Submit succeeded! (' + result + ') '); }})}//Dismiss event if (iframe.detachevent){iframe.detachevent (' onload ', function () {});                } else {Iframe.removeeventlistener (' Load ', function () {}, True);    }}}, 0); }) (document.getElementById (' Ajaxiframe '));</script>

Third, the combination of the above methods, now to expand the implementation of JavaScript;

<form id= FM name= FM action= server.aspx method= POST >    <input type= text name= value= title/ >    <input type= "button" value= "button" onclick= "Send (FM);"/>    commit Result: <span id= "Result" ></ Span></form>

JS CODE Public Method:

<script type= "Text/javascript" > Function XMLhttp () {this.request = null;        This.post = null;        iframe set var div = document.createelement (' div ');        div.innerhtml = ' <iframe id= "Ajaxiframe" name= "ajaxiframe" width= "0" height= "0" ></iframe> ";        Div.style.display = ' None ';        Document.body.appendChild (DIV);        var iframe = Div.firstchild; Post Set this.post = function (FM, URL, callback) {if (typeof url = = ' function ') {cal            Lback = URL; } if (typeof fm = = ' string ') {//String commit, instance: Ajax.send (' a=1&b=2 ', '/server.aspx ', function (data) {}                ); var f = document.createelement (' form ');                F.name = ' ajaxform ';                F.target = ' ajaxiframe ';                F.method = ' post ';                f.action = URL;                var ds = Fm.split ("&"); for (var i = 0; i < ds.length; i++) {iF (Ds[i]) {var v = ds[i];                        var el = document.createelement (' input ');                        El.type = ' hidden ';                        El.name = v.substr (0, v.indexof (' = '));                        El.value = V.substr (v.indexof (' = ') + 1);                    F.appendchild (EL);                }} document.body.appendChild (f);                F.submit ();            Document.body.removeChild (f); } else if (typeof fm = = ' object ') {//form submitted, instance: Ajax.send (document.getElementById (' FM '), '/server.aspx ', func                tion (data) {});                Fm.target = ' ajaxiframe ';                if (typeof url = = ' string ') {Fm.setattribute (' action ', URL);                } fm.submit ();                        if (Iframe.contentwindow) {//Bind event if (iframe.attachevent) { IE iframe.attachevent (' onLoad ', function () {this.result = Iframe.contentWindow.document.documentElement.innerText;                            if (typeof callback = = "function") {callback (This.result);                    }                        }); } else {Iframe.addeventlistener (' Load ', function () {This.result = IFR                            Ame.contentWindow.document.documentElement.textContent;                            if (typeof callback = = "function") {callback (This.result);                        }})}//Dismiss event if (iframe.detachevent) {                    Iframe.detachevent (' onload ', function () {});                    } else {Iframe.removeeventlistener (' Load ', function () {}, True); }                }            }        }    }   Xmlhttp.prototype = {send:function (FM, URL, callback) {AJAX.POST (FM, URL, callback);    }} var ajax = new XMLhttp (); function Send (s) {ajax.send (document.getElementById (' FM '), function (data) {document.getElementById ("R        Esult "). InnerHTML = data;        });    Ajax.send (' tit=string tit ', '/server.aspx ', function (data) {}); } </script>

Cross-Domain Commit

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.