Javascript 从java脚本调用rest api post服务引发错误
在控制台中从java脚本调用post服务时,我得到以下错误 加载资源失败:服务器响应状态为415() 在我的后端JavaEclipseIDE中出现了以下错误 内容类型为“文本/普通”;字符集=UTF-8'不受支持 如何解决此错误 提前谢谢 HTML代码:Javascript 从java脚本调用rest api post服务引发错误,javascript,java,html,spring-boot,spring-mvc,Javascript,Java,Html,Spring Boot,Spring Mvc,在控制台中从java脚本调用post服务时,我得到以下错误 加载资源失败:服务器响应状态为415() 在我的后端JavaEclipseIDE中出现了以下错误 内容类型为“文本/普通”;字符集=UTF-8'不受支持 如何解决此错误 提前谢谢 HTML代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <met
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="css/index.css" />
<title>Document</title>
</head>
<body>
<div>
<h1 class="myclass">HELLO!WELCOME</h1>
</div>
<div>
<form id="loginform" method="post">
<div class="form-group">
<input type="number" class="form-control" id="inputphone" placeholder="Enter phone number">
</div>
<div class="form-group">
<input type="password" class="form-control" id="inputpassword" placeholder="Enter Password">
</div>
<div class="forgot-password">
<a href="">forgot password</a>
</div>
<input type="submit"value="Login"/>
</form>
</div>
<script src="js/index.js"> </script>
</body>
</html>
你能分享后端代码吗?我的后端代码工作正常,而rest clientok的调用可能是这是问题的主体:JSON.stringify({jdata})应该是:JSON.stringify(jdata)是的,我尝试过,但没有使用相同的错误你不能使用
内容类型:application/JSON;charset=UTF-8
具有模式:“无COR”
,改为发送文本/普通
,后端拒绝发送。请参阅和。您能否共享后端代码我的后端代码工作正常,而来自rest clientok的调用可能是这是问题正文:JSON.stringify({jdata})应该是:JSON.stringify(jdata)是的,我尝试过,但没有使用相同的错误您不能使用内容类型:application/JSON;charset=UTF-8
具有模式:“无COR”
,改为发送文本/普通
,后端拒绝发送。见和。
var form = document.getElementById('loginform')
form.addEventListener('submit', function (e) {
e.preventDefault();
var inputphoneno = document.getElementById("inputphone").value;
var inputpassword = document.getElementById("inputpassword").value;
const jdata = {
"phonenumber":inputphoneno,
"password":inputpassword
};
fetch('http://localhost:8080/myurl/', {
method: 'POST',
body: JSON.stringify({jdata}),
headers: {
'Content-Type': 'application/json; charset=UTF-8',
},
mode: "no-cors"
})
.then(function (response) {
//return response.json();
console.log(response)
})
.then(function (data) {
console.log(data)
})
})