I was asked to develop a program that extracts the properties of each user profile. I'm using ASP.NET Core and Visual Studio and I'm trying to use AJAX and Javascript to do the requests. Here's my code.
<script>
var url = "https://[...]";
$(document).ready(function () {
$("button").click(function () {
$.ajax({
url: url + "/_api/web/lists",
crossDomain: true,
dataType: "jsonp",
method: "GET",
headers: {
"Accept": "application/jsonp; odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
},
success: function () {
alert("success");
},
error: function (xhr, ajaxOptions, thrownError) { //Add these parameters to display the required response
alert(xhr.status);
alert(xhr.responseJSON);
},
});
});
});
</script>
}
<div class="text-center">
<h1 class="display-4">Welcome</h1>
<p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
<button class="btn">Click</button>
</div>
However, like this, I can't access the API, as it returns a 403 error. How can I pass on my credentials so it recogninzes my login and what do I need to change here so I get a successful request?
Also, do I need to pass the script to a Sharepoint Add-in for this to work?
Thank you in advance