Using REST API and send POST request
11,295
Use jquery for this:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
And call function
$(document).ready(function(){
$.post('localhost:5000/registrar', {
"enrollId": "jim",
"enrollSecret": "6avZQLwcUe9b"
}, function(serverResponse){
//do what you want with server response
})
})
Same without shorthand to handle errors:
$.ajax({
type: "POST",
url: 'localhost:5000/registrar',
data: {
"enrollId": "jim",
"enrollSecret": "6avZQLwcUe9b"
},
success: function(){$('#register').html('<h1>Login successfull</h1>');},
error: function(){$('#register').html('<h1>Login error</h1>');},
dataType: dataType
});
Author by
CodeName
Updated on June 26, 2022Comments
-
CodeName almost 2 years
POST localhost:5000/registrar { "enrollId": "jim", "enrollSecret": "6avZQLwcUe9b" }
How do I use this in a javascript file? Do I use JSON or JQuery? And how do I invoke the request function in .html?
-
CodeName almost 8 yearsSo if I have a <div id="register"></div> in my .html, all I have to do is $(#register).append(serverResponse) under function(serverResponse)??
-
Denis Matafonov almost 8 yearswell, i Dont know what you server response is - but if it's html, then yes, you can
-
CodeName almost 8 years200 OK { "OK": "Login successful for user 'jim'." } This is the supposed response. @Денис Матафонов
-
Denis Matafonov almost 8 yearsYou don't need serverResponse object since it doesnt carry any valuebale info. If you need to handle failed login requests as well - you need to chacnge $.post function to $.ajax (see edit)
-
jhayjhay almost 5 yearscan i call this function in jsp?? if yes how?