Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/252.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 未发送XMLHttpRequest Post数据_Javascript_Php_Ajax_Query String - Fatal编程技术网

Javascript 未发送XMLHttpRequest Post数据

Javascript 未发送XMLHttpRequest Post数据,javascript,php,ajax,query-string,Javascript,Php,Ajax,Query String,这是javascript: function eAC(emailData) { if (window.XMLHttpRequest) { httpRequest = new XMLHttpRequest(); } if (!httpRequest) { return false; } console.log(emailData); var fd = new FormData(); fd.append("

这是javascript:

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

    if (!httpRequest) {
        return false;
    }

    console.log(emailData);

    var fd = new FormData();
    fd.append("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;
        }
    }
};
这是php:

$pec_result = array();

if(isset($_POST['email']) && strlen($_POST['email']) > 0){
    $pec_result['error'] = 'Its good';
    echo json_encode($pec_result);
    die();
} else {
    $pec_result['error'] = $_POST['email'];
    echo json_encode($pec_result);
    die();
}
这里的问题是
$\u POST['email']
的值为
NULL
。当
emailData
的console.log()返回值时,$\u POST['email']为什么为空。有人能帮忙吗?我认为问题在于附加部分。(不确定)

请不要使用jQuery。我知道如何在jQuery中做到这一点,但我想学习如何在javascript中做到这一点。所以是的,谢谢

试试这个(编辑)

完整代码:

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;
        }
    }
};

您的问题是,使用
FormData
时,请求以
multipart/form data
not
application/x-www-form-urlencoded
的形式发送。删除设置内容类型的行。当您将
FormData
对象传递给
XMLHttpRequest.send

时,将自动设置正确的内容类型。您使用的浏览器是什么?感谢它现在工作正常。但是为什么
newformdata()
在代码中不起作用呢?Moz文档在他们的示例中使用了它,我不知道,但让我们想想为什么jquery有write.serialize?这表明我们应该以查询字符串格式发送数据。我已经检查了链接,我唯一能猜到的是你也应该在FireFox上尝试。我认为你的解决方案以及mozilla在fire fox中的工作。你得到答案了吗?是的,我得到了答案。也许这是一个浏览器的东西。它没有在页面中指定。哦,好吧。再次感谢!
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;
        }
    }
};