英文:
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 = "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")));
}
My javascript functions:
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);
}
答案1
得分: 0
问题已解决。
是因为我使用了
英文:
The problem was solved.
It was that I used the button tag to send the user request, changed it to the button type input and everything was fixed
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论