How do you send a post-request to the earring through javascript?



  • I'm writing a web service interested in the next scenario.

    1. There's a html file with the table and the button.
    2. When selecting the field, a new category of "active" is assigned to the field in the table.
    3. Press the button.
    4. I want to get the data in the first column of the chosen field.

    The question is, how do we arrange a post-request to the earring through js and send the data?

    If we take the data from the form, it's all right.

    I'd rather not use the form, but I'd like to make sure that all of Html's file shows up and sends the necessary data.

    Script file:

    function getXmlHttp() {
        var xmlHttp = null;
        if (window.XMLHttpRequest) {
            xmlHttp = new XMLHttpRequest();
        }
        else if (window.ActiveXObject) {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        return xmlHttp;
    }
    

    function httpReq(URL, method, data, success, error) {
    var request = getXmlHttp();
    request.open(method, URL, true);
    request.setRequestHeader("Content-type", "multipart/form-data");
    request.send(data);
    request.onreadystatechange = function() {
    if (request.readyState == 4) {
    if (request.status == 200) {
    success(request.responseText);
    }
    else {
    if (error) error(request.status);
    }
    }
    }
    }

    var table = document.querySelector("#selectTable")
    var field = table.querySelector(".active");
    var item = field.children[0]; // первый дочерний

    var form = new FormData();
    form.append("some_key",
    item.innerHTML); // ну или что там нужно вытащить
    httpReq("/admin", "POST", form, function(res) {
    console.log("response:", res);
    });



  • function getXmlHttp() {
        var xmlHttp = null;
        if (window.XMLHttpRequest) {
            xmlHttp = new XMLHttpRequest();
        }
        else if (window.ActiveXObject) {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        return xmlHttp;
    }
    
    function httpReq(URL, method, data, success, error) {
        var request = getXmlHttp();
        request.open(method, URL, true);
        request.setRequestHeader("Content-type", "multipart/form-data");
        request.send(data);
        request.onreadystatechange = function() {
            if (request.readyState == 4) {
                if (request.status == 200) {
                    success(request.responseText);
                }
                else {
                    if (error) error(request.status);
                }
            }
        }
    }
    
    
    someBtn.onclick = function(e) {
        var table = document.querySelector("селектор таблицы")
        var field = table.querySelector(".active");
        var item = field.children[0];  // первый дочерний
    
        var form = new FormData();
        form.append("some_key",
                item.innerHTML);  // ну или что там нужно вытащить
        form.append("another_key", another_value);
    
        httpReq("url...", "POST", form, function(res) {
            console.log("response:", res);
        }, function(err) {
            console.error(err);
        })
    };
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2