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
});
Share:
11,295
CodeName
Author by

CodeName

Updated on June 26, 2022

Comments

  • CodeName
    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
    CodeName almost 8 years
    So 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
    Denis Matafonov almost 8 years
    well, i Dont know what you server response is - but if it's html, then yes, you can
  • CodeName
    CodeName almost 8 years
    200 OK { "OK": "Login successful for user 'jim'." } This is the supposed response. @Денис Матафонов
  • Denis Matafonov
    Denis Matafonov almost 8 years
    You 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
    jhayjhay almost 5 years
    can i call this function in jsp?? if yes how?