Pass Parameters To XMLHttpRequest Object

前端 未结 3 1701
野性不改
野性不改 2021-01-01 21:10

How can I pass parameters to the XMLHttpRequest Object?

function setGUID(aGUID) {

    var xhReq = new XMLHttpRequest();

    xhReq.open(\"POST\", \"ClientS         


        
相关标签:
3条回答
  • 2021-01-01 21:50

    There's a lot of tutorials about "xmlhttprequest post" on the internet. I just copy one of then:

    Take a look:

    http://www.openjs.com/articles/ajax_xmlhttp_using_post.php

    https://www.google.com/search?q=xmlhttprequest+post

    var http = new XMLHttpRequest();
    var url = "url";
    var params = JSON.stringify({ appoverGUID: approverGUID });
    http.open("POST", url, true);
    
    http.setRequestHeader("Content-type", "application/json; charset=utf-8");
    http.setRequestHeader("Content-length", params.length);
    http.setRequestHeader("Connection", "close");
    
    http.onreadystatechange = function() {
        if(http.readyState == 4 && http.status == 200) {
            alert(http.responseText);
        }
    }
    http.send(params);
    
    0 讨论(0)
  • 2021-01-01 22:02

    This way works for me.

    var data = new FormData();
    data.append('parameter_1', 'value parameter 1');
    data.append('parameter_2', 'value parameter 2');
    ...
    
    var xhr = new XMLHttpRequest();
    xhr.open('POST', 'URL', true);
    xhr.onload = function () {
        console.log(this.responseText);
    };
    xhr.send(data);
    
    0 讨论(0)
  • 2021-01-01 22:08

    Just remove these two lines (which are not allowed to set these headers):

    http.setRequestHeader("Content-type", "application/json; charset=utf-8");
    xmlHttp.setRequestHeader("Connection", "close");
    
    0 讨论(0)
提交回复
热议问题