使用REST API并发送POST请求

Cod*_*ame 1 html javascript jquery json

POST localhost:5000/registrar

{
  "enrollId": "jim",
  "enrollSecret": "6avZQLwcUe9b"
}
Run Code Online (Sandbox Code Playgroud)

我如何在javascript文件中使用它?我使用JSON还是JQuery?我如何在.html中调用请求函数?

Den*_*nov 5

使用jquery:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
Run Code Online (Sandbox Code Playgroud)

并呼叫功能

$(document).ready(function(){

    $.post('localhost:5000/registrar', {
      "enrollId": "jim",
      "enrollSecret": "6avZQLwcUe9b"
    }, function(serverResponse){

    //do what you want with server response

    })

})
Run Code Online (Sandbox Code Playgroud)

同样没有简写来处理错误:

$.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
});
Run Code Online (Sandbox Code Playgroud)