I seem to be going around in circles on this and from my searches, I seem to only be able to find solutions for more "complex" problems.
I want to pass an int and a JSON array of strings to the API and then process them. However, it says the string is in the wrong format. So I tried dumbing it down to just deal with a simple string, however it just returns as null. The id is picked up fine and passes that back if I change the return to the id, but with items, it always says it is null.
Looking at the request, the payload looks fine.
Javascript (JQuery)
function saveHealthCheck() {
var cID = $('#<%= hfHealthCheck.ClientID %>').val();
var jsonstr = '"Hello World!"'
$.ajax({
url: '<%= ResolveUrl("~/api/AppliesTo/") %>' + cID,
dataType: 'application/json',
data: jsonstr,
type: 'PUT',
success: function (data) {
alert(data);
}
});
}
C# API
public string Put(int id, [FromBody] string items)
{
//Just to test
return items;
}
2
Answers
I am not sure why this is different to what others have suggested, but this works.
Javascript
API
Model
Thank you for the input in solving this.
You can’t pass string as object via POST or PUT. Your option is to wrap it into a data object.
Updated JS script just for example:
Data model class:
Updated Api: