Javascript 像curl一样发送post数据

Javascript 像curl一样发送post数据,javascript,html,curl,boost-asio,Javascript,Html,Curl,Boost Asio,我使用这个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="f

我使用这个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>



函数xhrSubmit(){ var file_obj=document.getElementById('fafafafa').files[0]; var fd=新FormData(); fd.append('fafafa',file_obj); xhr=newXMLHttpRequest(); xhr.open('POST','https://192.168.1.10:8283/here",对) setRequestHeader('Content-type','application/json'); xhr.send(fd); xhr.onreadystatechange=函数(){ if(xhr.readyState==4){ var obj=JSON.parse(xhr.responseText); 控制台日志(obj); } }; }
但是当我使用Web服务器的网页时,我得到了它所期望的对象或数组的响应

如何更改javascript代码以发送数据(如curl命令),将文件发布到Web服务器时不会出现问题

我的服务器端的完全boost异常是

Exception <unspecified file>(1): expected object or array
异常(1):预期的对象或数组

我想说,检查一下您收到的curl版本。它不能是应用程序/json和文件数据。您需要多部分/表单数据。

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


尝试将内容类型设置为Apdio/XWW-Fras-UrLnCalp.

最后,问题是JSON有效消息和卷发中的许多新行不关心浏览器,

您的服务器端代码是什么?这是我自己开发的代码,它是在C++中的Boost之上构建的。它适用于wget和curl命令的下载和上传,但我想尝试使用一个网页。使用这种内容类型并没有改变任何东西。我相信您应该尝试阅读HTTP头和XHR的基本原理。很明显,你是在随机尝试让事情顺利进行,但这不是一条路,永远:-)。在curl中,我刚刚发现我可以用内容类型写任何东西,而且它仍然有效