2020-04-25

jQuery - Ajax call example

<!DOCTYPE html>
<html>
<head>
<script
  src="https://code.jquery.com/jquery-1.12.4.min.js"
  integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ="
  crossorigin="anonymous"></script>
</head>
<body>

<script>
$.ajax({
url: "https://jsonplaceholder.typicode.com/todos/1",
beforeSend: function(xhr) {
//xhr.setRequestHeader("Authorization", "Basic " + btoa("username:password"));
xhr.setRequestHeader("Content-Type", "application/json")
},
type: "POST",
dataType: "json",
contentType: "application/json",
processData: false,
data: '{"foo":"bar"}',

success: function (response) {
alert(JSON.stringify(response));
},

error: function(){
alert("Error!");
}
});
</script>


</body>
</html>

No comments:

Post a Comment