pquest后数据XMLHtt $ P $送交 [英] XMLHttpRequest Post Data not being sent

查看:136
本文介绍了pquest后数据XMLHtt $ P $送交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是JavaScript:

This is the 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();
}

这里的问题是, $ _ POST [电子邮件] NULL 的值。为什么CONSOLE.LOG()时为 emailData 返回值是$ _ POST [电子邮件]空。任何人都可以帮忙吗?我认为,概率是在附加部件。 (不知道)

The problem here is that $_POST['email'] has a value of NULL. Why is $_POST['email'] null when console.log() for emailData returns a value. Can anyone help? I think the prob is in the appending part. (Not sure)

请没有的jQuery 。我知道如何做到这一点的jQuery的,但我想学习如何做到这一点的JavaScript。所以呀谢谢

Please no jQuery. I know how to do this in jQuery but I want to learn how to do this in javascript. So yeah thanks

推荐答案

您的问题是与 FORMDATA 请求被发送的的multipart /表单-data 不是应用程序/ x-WWW的形式urlen codeD 。删除你设置的内容类型的线。正确的内容类型时,会自动传递 FORMDATA 对象设置 XMLHtt prequest.send

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.

这篇关于pquest后数据XMLHtt $ P $送交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆