i always get this error when i type in the userid, password, usertype. i cant figure it out. but i see it is in line 72 and 73
i dont know how the promise work
this here is the code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Fetch API Sandbox</title>
</head>
<body>
<h1>Fetch API Sandbox</h1>
<button id="getUsers">Get JSON</button>
<div id="output"></div>
<!--Hier wird das JSON reingeschrieben-->
<form id="addPost">
<div> <input type="text" id="userid" placeholder="userID"> </div>
<div> <input type="text" id="password" placeholder="password"> </div>
<div> <input type="text" id="usertype" placeholder="userType"> </div>
<input type="submit" value="Submit">
</form>
<script>
document.getElementById('getUsers').addEventListener('click', getUsers);
document.getElementById('addPost').addEventListener('submit', addPost);
function getUsers() {
fetch('https://192.168.50.34:9004/v1/login',{mode: "no-cors"})
.then(function(response) {
if (response.ok)
return response.json();
else
throw new Error("Names could not be loaded!")
})
.then(function(json) {
console.log(json);
let output = '<h2>Users</h2>';
json.forEach(function(user) {
output += `
<ul>
<li>userID: ${user.userid}</li>
<li>password: ${user.password}</li>
<li>userType: ${user.usertype}</li>
</ul>
`;
console.log(output);
document.getElementById("output").innerHTML = output;
});
})
}
function addPost(e) {
e.preventDefault();
let userId = document.getElementById('userid').value;
//console.log(id);
let password = document.getElementById('password').value;
let userType = document.getElementById('usertype').value;
fetch('https://192.168.50.34:9004/v1/login', {
method: 'POST',
mode: 'no-cors',
headers: {
'Accept': 'application/json,',
'Content-type': 'application/json'
},
body: {
userId: userId,
password: password,
userType: userType
}
})
.then((res) => res.json())
.then((data) => console.log(data))
}
</script>
</body>
</html>
i have tried to hardcode the userid and the password and usertype, i can see that the error come from
.then((res) => res.json())
.then((data) => console.log(data))
but i dont exactly understand what the code is doing
the error i get from Google Chrome is
Uncaught (in promise) SyntaxError: Unexpected end of input (at index.html:72:22)
at index.html:72:22
(anonymous) @ index.html:72
Promise.then (async)
addPost @ index.html:73
this here is via postman how the server should response
{
"Result": {
"ResultCode": 0
},
"AccountInfo": {
"UserID": 1000000000000000000,
"UniqueID": "",
"Uuid": "xxxx-xxxx-xxxx-xxxx",
"Name": "xxxx",
"LoginPW": "",
"Privilege": 1,
"FirstLoginFlag": 0,
"ServerID": 11,
"LoginAllowed": 1,
"LoginFailCount": 0,
"WebLogin": 1,
"VisitLogin": 0,
"LoginIP": "",
"LoginTime": "2022-12-21 12:45:56",
"LastLoginIP": "",
"LastLoginTime": "2022-12-21 12:08:12"
},
"SystemInfo": {
"Version": "1.1.17.0",
"LicenseLevel": 40,
"LicenseStatus": 0,
"BrandType": 1,
"TimezoneVersion": 0,
"HTTPSFlag": 1,
"SiteName": "",
"SiteLogo": "",
"SiteMessageWarning": "",
"DeviceServerVersion": 10
},
"LoginFailInfo": {
"RemindCount": 0
}
}
any help and links are very appreciated.