发送像curl这样的帖子数据

问题描述 投票:0回答:3

我使用此curl命令将文件推送到Web服务器

curl -i -X POST -H 'Content-Type: application/json' -d @./mylocalfile -k https://192.168.1.10:8283/here

我想使用一个简单的网页,而不是我可以选择mylocalfile,我有这个

<html>
<body>
    <input class="file" type="file" id="fafafa" name="fileupload" /><br/>
    <br/>
    <input type="button" value="Submit" onclick="xhrSubmit();" />
    <script type="text/javascript ">
        function xhrSubmit() {
            var file_obj = document.getElementById('fafafa').files[0];
            var fd = new FormData();
            fd.append('fafafa', file_obj);
            xhr = new XMLHttpRequest();
            xhr.open('POST', 'https://192.168.1.10:8283/here', true)
            xhr.setRequestHeader('Content-type', 'application/json');
            xhr.send(fd);
            xhr.onreadystatechange = function() {
                if (xhr.readyState == 4) {
                    var obj = JSON.parse(xhr.responseText);
                    console.log(obj);
                }
            };
        }
    </script>
</body>
</html>

但是,当我使用网页时,我得到了它预期的对象或数组的响应。

如何更改javascript代码以发送数据,如curl命令,将文件发布到Web服务器没有问题?

我服务器端的完全提升异常是

Exception <unspecified file>(1): expected object or array
javascript html curl boost-asio
3个回答
1
投票

您的内容类型错误。您没有将JSON发送到您的服务器。

尝试将content-type设置为application / x-www-form-urlencoded。


0
投票

我会说,检查你收到的curl版本。它不能是application / json和文件数据。您需要multipart / form-data。


0
投票

最后,问题在于json有效负载消息和curl中的许多换行符并不关心浏览器。

© www.soinside.com 2019 - 2024. All rights reserved.