function upload() { var form = document.getElementById( "load" ); var obj = {"name":"", "runtimeAttributes":{ "language":"", "code":"" }}; var elements = form.querySelectorAll( "input, select" ); obj.name = elements[0].value; r = obj.runtimeAttributes; r.language = elements[1].value; file = document.getElementById("code").files[0]; var reader = new FileReader(); reader.onloadend = function(event) { var content = event.target.result; r.code = content var json = JSON.stringify( obj, null, ' '); output.innerHTML = json; var xhr = new XMLHttpRequest(); xhr.open(form.method, form.action, true); xhr.setRequestHeader('Content-Type','application/json'); xhr.send(json); }; reader.readAsText(file); } 

 <form id = "load" method="post" action="http://localhost:8080/lambdas" onsubmit="javascript:upload()" enctype="application/json"> <input type="text" name="name" value="helloworld"> <select name="language"> <option>Python3</option> </select> <input type="file" name="code" id = "code"> <input type="submit" value="send"> </form> 

 {"message":"org.springframework.web.HttpMediaTypeNotSupportedException Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported"} 
  • Try Content-type - vp_arth
  • the same .... - Mavriky Nikolayevich
  • What is in json variable? - andreymal
  • try Content-Type: application/x-www-form-urlencoded - cmd
  • in json of this type, the object is {"name": "", "runtimeAttributes": {"language": "", "code": ""}} var json = JSON.stringify (obj, null, ''); - Mavriky Nikolaevich

1 answer 1

There are no errors in the code given by you, here is a working example:

 var xhr = new XMLHttpRequest(); xhr.open('POST', 'http://httpbin.org/post', true); xhr.setRequestHeader("Content-Type", "application/json"); xhr.onreadystatechange = function() { if (xhr.readyState != 4) return; if (xhr.status != 200) { console.log(xhr.status + ': ' + xhr.statusText); } else { console.log(xhr.responseText); } } xhr.send(JSON.stringify({param: 'value'})); 

As you can see, the server processed the request correctly, and the correct Content-Type present in the headers.

Looking at all? you have another Content-Type header on your server, or a few of them, look in the code.

You can see the actual request headers in DevTools : F12 - вкладка Network(Сеть)

  • few definitely not. Is it possible for dummies?) How can another headline go away? - Mavriky Nikolaevich
  • Have you looked at DevTools? What is leaving? If not application/json , look somewhere in the code you set setRequestHeader . xhr do you have a global variable? Or is the whole thing wrapped in a function? - vp_arth
  • xhr in function, devtools in request is application / x-www-form-urlencoded - Mavriky Nikolaevich
  • Well, now look for who writes it there) - vp_arth
  • uploaded all the code, no longer know where to look - Mavriky Nikolaevich