CodeName
CodeName

Reputation: 579

Using REST API and send POST request

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?

Upvotes: 2

Views: 12037

Answers (1)

Denis Matafonov
Denis Matafonov

Reputation: 2812

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
});

Upvotes: 5

Related Questions