无效的内容类型:application x www form-urlencoded

huangapple go评论78阅读模式
英文:

invalid content type: application x www form-urlencoded

问题

以下是您提供的内容的翻译部分:

当我将 RequestHeader 设置为 Content-type = "application/json" 时,它会更改为 'application/x-www-form-urlencoded',然后我收到不支持的媒体类型错误(无效的内容类型:application/x www form-urlencoded)

我的 RestController 代码:

@PostMapping(value = "books/searchProcess/{pageNumber}")
@ResponseBody
public List<BookDto> bookSearchProcess(@RequestBody BookDto bookDto,
                                       @PathVariable(required = false) int pageNumber) {
    long totalPages = bookService.getTotalNumberOfPages(bookDto);
    if (totalPages == 0)
        return null;
    if (pageNumber > totalPages)
        pageNumber = (int) totalPages;
    return bookService.findMaxMatch(bookDto, pageNumber - 1, Integer.parseInt(env.getProperty("Page.Rows")));
}

我的 JavaScript 函数:

function sendToSearch(pageNumber) {
    let book = createJsonSearchObject();
    const request = new XMLHttpRequest();
    request.onreadystatechange = function () {
        if (this.readyState === 4 && this.status === 200) {
            createResultTable(this.response);
        }
    };
    request.open("POST", "/admin/books/searchProcess/" + pageNumber, true);
    request.setRequestHeader("Content-type", "application/json");
    request.dataType = "json";
    request.responseType = "json";
    book = JSON.stringify(book);
    request.send(book);
}

function createResultTable(books) {
    let table = document.createElement('table');
    table.setAttribute("id", "result-table");
    table.setAttribute("class", "table table-hover table table-bordered table-striped");
    let i;
    table.insertRow(0).outerHTML =
        `<tr class="header">
            <th>Book Name</th>
            <th>Author Name</th>
            <th>Author Family</th>
            <th>ISBN</th>
        </tr>`;
    for (i = 0; i < Object.keys(books).length; i++) {
        const book = books[i];
        table.insertRow(i + 1).outerHTML =
            `<tr>
                <td id="name` + i + `">` + book.name + `</td>
                <td id="author.name` + i + `">` + book.author.name + `</td>
                <td id="author.family` + i + `">` + book.author.family + `</td>
                <td id="isbn` + i + `">` + book.isbn + `</td>
            </tr>`;
    }
    const deleteBtn = document.createElement('button');
    deleteBtn.setAttribute("class", "btn btn-group btn-danger btn-delete");
    deleteBtn.setAttribute("id", "btn-delete");
    deleteBtn.setAttribute("onclick", "sendToDelete()");
    deleteBtn.appendChild(document.createTextNode("Delete Selected Items"));
    document.getElementById("search-result").append(table);
    document.getElementById("delete-btn").append(deleteBtn);
}
英文:

when I set RequestHeader to Content-type = "application/json", it change to 'application/x-www-form-urlencoded' and I got Unsupported Media Type error (invalid content type: application/x www form-urlencoded)

My RestController code:

 @PostMapping(value = &quot;books/searchProcess/{pageNumber}&quot;)
@ResponseBody
public List&lt;BookDto&gt; bookSearchProcess(@RequestBody BookDto bookDto,
@PathVariable(required = false) int pageNumber) {
long totalPages = bookService.getTotalNumberOfPages(bookDto);
if (totalPages == 0)
return null;
if (pageNumber &gt; totalPages)
pageNumber = (int) totalPages;
return bookService.findMaxMatch(bookDto, pageNumber - 1, Integer.parseInt(env.getProperty(&quot;Page.Rows&quot;)));
}

My javascript functions:

function sendToSearch(pageNumber) {
let book = createJsonSearchObject();
const request = new XMLHttpRequest();
request.onreadystatechange = function () {
if (this.readyState === 4 &amp;&amp; this.status === 200) {
createResultTable(this.response);
}
};
request.open(&quot;POST&quot;, &quot;/admin/books/searchProcess/&quot; + pageNumber, true);
request.setRequestHeader(&quot;Content-type&quot;, &quot;application/json&quot;);
request.dataType = &quot;json&quot;;
request.responseType = &quot;json&quot;;
book = JSON.stringify(book);
request.send(book);
}
function createResultTable(books) {
let table = document.createElement(&#39;table&#39;);
table.setAttribute(&quot;id&quot;, &quot;result-table&quot;);
table.setAttribute(&quot;class&quot;, &quot;table table-hover table table-bordered table-striped&quot;);
let i;
table.insertRow(0).outerHTML =
`&lt;tr class=&quot;header&quot;&gt;
&lt;th&gt;Book Name&lt;/th&gt;
&lt;th&gt;Author Name&lt;/th&gt;
&lt;th&gt;Author Family&lt;/th&gt;
&lt;th&gt;ISBN&lt;/th&gt;
&lt;/tr&gt;`;
for (i = 0; i &lt; Object.keys(books).length; i++) {
const book = books[i];
table.insertRow(i + 1).outerHTML =
`&lt;tr&gt;
&lt;td id=&quot;name` + i + `&quot;&gt;` + book.name + `&lt;/td&gt;
&lt;td id=&quot;author.name` + i + `&quot;&gt;` + book.author.name + `&lt;/td&gt;
&lt;td id=&quot;author.family` + i + `&quot;&gt;` + book.author.family + `&lt;/td&gt;
&lt;td id=&quot;isbn` + i + `&quot;&gt;` + book.isbn + `&lt;/td&gt;
&lt;/tr&gt;`;
}
const deleteBtn = document.createElement(&#39;button&#39;);
deleteBtn.setAttribute(&quot;class&quot;, &quot;btn btn-group btn-danger btn-delete&quot;);
deleteBtn.setAttribute(&quot;id&quot;, &quot;btn-delete&quot;);
deleteBtn.setAttribute(&quot;onclick&quot;, &quot;sendToDelete()&quot;);
deleteBtn.appendChild(document.createTextNode(&quot;Delete Selected Items&quot;));
document.getElementById(&quot;search-result&quot;).append(table);
document.getElementById(&quot;delete-btn&quot;).append(deleteBtn);
}

答案1

得分: 0

问题已解决。
是因为我使用了

huangapple
  • 本文由 发表于 2020年10月20日 23:24:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/64448324.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定