Use REST API from javascript client in custom page

This public forum is for user-to-user discussions of ASP.NET Maker. Note that this is not support forum.
Post Reply
Andros
User
Posts: 111

Use REST API from javascript client in custom page

Post by Andros »

Hi, I have a "photos" table, and a custom page which shows some photos. With every photo there is an icon to delete it.
I want to let the user to click the icon, request a confirmation, and if yes deletes the image and refreshes the page.
The click generates a 401 Unauthorized error. Do I have to create the JWT? Is there any way to reuse the current sessione JWT?

var photos=ExecuteRows("SELECT * FROM photos;");
foreach (Dictionary<string, object> img in photos) {
@<p>
@img["PhotoTitle"]
<a href="#" class="deleteimg" data-id="@img["PhotoID"]"><i class="fa fa-trash"></i></a>
</p>
}

<script>
$('.deleteimg').click(function(){
			$.get("api/delete/photos/" + $(this).data('id'), function(res) {
				if (res && res.success) {
					var row = res[object];
					alert(JSON.stringify(row)); // Show output JSON
                                        location.reload();
				} else {
					alert(res.failureMessage);
				}
			});
		});
</script>

MichaelG
User
Posts: 1110

Post by MichaelG »

For API request, you should always use the JWT Token. Please read:
https://aspnetmaker.dev/docs/#/api.html ... -web-token

If you have enabled Include common files for your custom file, you should be able to retrieve the JWT token from: ew.API_JWT_TOKEN


Post Reply