javascript - 发布请求被切断

标签 javascript ajax node.js

我的 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)(*&&^%$

作为用户名,它被 chop 为

    >df)(* 

我怀疑这是因为&。

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

提前致谢!

最佳答案

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

关于javascript - 发布请求被切断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42951151/

相关文章:

node.js - Redis docker-compose 与 Nodejs - 连接 ENOENT

javascript - 在 "Pro Javascript for Web Dev"书中找不到 server.js

javascript - 使用 React-dnd 的 MUI TreeView

当我打开与 CodeSleeve Assets 管道的串联时,Javascript 不工作(Laravel 4)

javascript - 推送到 Analytics 的人工综合浏览量正在改变 PPC 流量来源

javascript - 使用 jQuery AJAX GET 和 AngularJS 不更新 View

node.js - Jade 模板布局不能与 Node.js 结合使用

java - 如何将JSP页面的一部分导出到Excel表格

javascript - bootpag 事件在返回的 Ajax 函数之后使用时会运行多次

node.js - MongoDB:mongos 进程、node-mongodb-native、connect-session-mongo