【问题标题】:Post request gets cut off发布请求被切断
【发布时间】:2017-08-14 12:54:38
【问题描述】:

我的 javascript 发布请求有问题。

我有以下代码用于发送发布请求

    var xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function() 
        {
            if (this.readyState == 4 && this.status == 200) 
            {   
                //stuff
            }
        };
        xhttp.open("POST", "/deleteUser", true);
        xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xhttp.send("username=" + username);

在我的 node.js express 应用程序中,我像这样收到它

    var username = req.body.username;

现在这对普通字符(数字和字母)非常有效。但是当我尝试

>df)(*&&^%$

作为用户名,它被切断

    >df)(* 

我怀疑这是因为 &。

我的问题:我如何防止它被切断。

提前致谢!

【问题讨论】:

标签: javascript ajax node.js


【解决方案1】:

您需要将username 参数转换为URI 安全字符串,使用xhttp.send("username="+encodeURIComponent(username));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-06
    相关资源
    最近更新 更多