简体   繁体   中英

Submit form on another page using javascript/ajax

I want to submit a form which is on another page using ajax. Here is my code:

Send();
function Send() {
var abc = document.getElementsByClassName("main");
for (var i = 0; i < abc.length; i++) {
    var item = abc.item(i);
            var mainButton = item.getElementsByClassName("mybuttonclass");
            if (mainButton.length > 0) {
                if (mainButton[0].innerText.includes("some text")){

                    var url = mainButton[0].getAttribute("href");                
                    loadXMLDoc(url, title, text);
                }

            }
        }  
    }

function loadXMLDoc(url, title, text) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.responseType = "document";
xmlhttp.onreadystatechange = function () {
    if (xmlhttp.readyState == XMLHttpRequest.DONE) {
        if (xmlhttp.status == 200) {
                var resp = xmlhttp.response;
                var textArea = resp.getElementsByClassName('textarea');                   
                 textArea.value = "Hello, this is my test message!";                     
                 var subBtn = resp.getElementsByClassName('btnclass');
                 if (subBtn.length > 0) {
                     subBtn.click();
                 }                
        }
        else if (xmlhttp.status == 400) {
            console.log('There was an error 400');
        }
        else {
            console.log('something else other than 200 was returned');
        }
    }
};

xmlhttp.open("POST", url, true);
xmlhttp.send();
}

This isn't working as expected, nothing is sent in message, no error is logged. What I am missing? How this can be achieved. Any help is appreciated.

Thanks.

The refresh should be done by the server when the page is loaded. If that isn't an option, I would use jQuery's $.ajax method in synchronous mode, attacked to the tab's click event:

$('a.tab').click(function() {
    $.ajax({
        url: "/refresh",
        method: "post",
        async: false,
        data: { refresh: "ok" }
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM