0
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"}
6
  • Попробуй Content-type
    – vp_arth
    28 фев 2017 в 16:05
  • то же самое.... 28 фев 2017 в 16:33
  • Что находится в переменой json?
    – andreymal
    28 фев 2017 в 16:54
  • попробуйте Content-Type: application/x-www-form-urlencoded
    – cmd
    28 фев 2017 в 16:54
  • в json такого вида объект {"name":"", "runtimeAttributes":{ "language":"", "code":"" }} var json = JSON.stringify( obj, null, ' '); 28 фев 2017 в 16:55

1 ответ 1

0

В приведённом вами коде нет ошибок, вот рабочий пример:

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'}));

Как видим, сервер обработал запрос корректно, и в заголовках присутствует корректный Content-Type.

Судя по всему? у вас на сервер уходит другой заголовок Content-Type, либо их несколько, ищите в коде.

Можно посмотреть актуальные заголовки запроса в DevTools: F12 - вкладка Network(Сеть)

13
  • нескольких точно нет. а можно для чайников?) как может уходить другой заголовок? 28 фев 2017 в 16:52
  • Вы в DevTools посмотрели? Что уходит? Если не application/json, ищите где-то в коде вы выставляете setRequestHeader. xhr у вас - глобальная переменная? Или всё это дело в функцию таки завёрнуто?
    – vp_arth
    28 фев 2017 в 17:12
  • xhr в функции, в devtools в request стоит application/x-www-form-urlencoded 28 фев 2017 в 17:17
  • Ну вот, теперь ищите, кто его туда пишет)
    – vp_arth
    28 фев 2017 в 17:17
  • загрузил весь код, уже не знаю, куда смотреть 28 фев 2017 в 17:24

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.