XMLHttpRequest Post Data not being sent

后端 未结 2 970
清歌不尽
清歌不尽 2021-01-18 02:35

This is the javascript:

function eAC(emailData) {
    if (window.XMLHttpRequest) {
        httpRequest = new XMLHttpRequest();
    }

    if (!httpRequest) {         


        
相关标签:
2条回答
  • 2021-01-18 03:17

    Your problem is that with FormData the request is sent as multipart/form-data not application/x-www-form-urlencoded. Remove the line where you set the content type. The correct content type will be set automatically when you pass a FormData object to XMLHttpRequest.send.

    0 讨论(0)
  • 2021-01-18 03:29

    try this (EDITED)

    fd='email='+emailData;
    

    full code:

    function eAC(emailData) {
        if (window.XMLHttpRequest) {
            httpRequest = new XMLHttpRequest();
        }
    
        if (!httpRequest) {
            return false;
        }
    
        console.log(emailData);
    
    //    var fd = new FormData();
    //    fd.append("email", emailData);
          fd='email='+emailData;
    
        httpRequest.onreadystatechange = eAC_callback; 
        httpRequest.open('POST', "http://website.com/file.php");
        httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        httpRequest.send(fd);
    }
    
    function eAC_callback() {
        if (httpRequest.readyState === 4) {
            if (httpRequest.status === 200) {
                var response = JSON.parse(httpRequest.responseText);
                    console.log(response);
            } else {
                return false;
            }
        }
    };
    
    0 讨论(0)
提交回复
热议问题