1

I have an html form from which I need to collect data and call a POST on a rest api. I am trying to do this using javascript and $.ajax but confused on how to setup the URL and collect data as I am very new to it. Could someone explain this fully, with an example if possible as I'm having trouble finding detailed documentation.

2
  • can you please add your code , what you done earlier. Commented Jan 6, 2021 at 7:10
  • Welcome to StackOverflow. Others may be able to help you better if you can include example code of what you are trying to accomplish and what you expect the result to be. See stackoverflow.com/help/how-to-ask for additional guidance. Commented Jan 6, 2021 at 7:28

2 Answers 2

3

Try this:

<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<input type="text" value="3" name="id" id="GetId">
<button id="LoginBtn">login</button>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"> 
</script>
<script>
    $("#LoginBtn").click(function (e){
    e.preventDefault();
    var settings = {
        "url": "http://192.168.1.3:8071/user/login",
        "method": "POST",
        "headers": {
            "Content-Type": "application/json"
        },
        "data": JSON.stringify({"username":"user","password":"123456"}),
    };
    $.ajax({
        ...settings,
        success: function (result) {
            alert("success")
        },
        error : function (){
            alert("error")
        }
    })
})
</script>
</body>
</html>
Sign up to request clarification or add additional context in comments.

Comments

0

Please read this simple article https://javascript.info/fetch You could use axios or jquery but fetch api is really simple

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.