I'm trying to solve an error that I'm running into in an ajax call. I've console logged the error and the error is as follow:
{"Message":"Invalid JSON primitive: .","StackTrace":"
at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject()\r
at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)\r\n
at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeDictionary(Int32 depth)\r\n
at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)\r\n
at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)\r\n
at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)\r\n
at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)\r\n
at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.ArgumentException"}
Here is my call on the front end:
$.ajax({
type: "POST",
url: "thePage.aspx/theFunction",
data: "{status: " + status + ", appointmentRequestQueueId: " + parameter + ", userId: " + userId + "}",
async: false,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
return;
},
error: function (request) {
console.log(request.responseText);
alert("An error has occurred!");
}
});
And here is my backend code:
[WebMethod]
public static void UpdateDocumentStatus(int status, int appointmentRequestQueueId, int userId)
{
var mgr = ScheduledEventManagerFactory.CreateSchedulingManager(
new SourceInformation(SystemSourceTypes.Intranet, userId, GetConfigSettingStatic("LangCulture")));
if( status == 0 ) {
mgr.UpdateDocumentStatus(appointmentRequestQueueId, null);
} else {
mgr.UpdateDocumentStatus(appointmentRequestQueueId, status);
}
}
Question: What character or string is it saying is invalid here: the white space ''? Empty string?
I've found a similar question here: message shows undefined when ajax post in ASP.net //similar issue
But the solution here sounds as if this is happening in an MVC solution, possibly an API, but my project is neither.
I've tried changing the ajax to a fetch but I'm not sure I have my syntax correct.
function SaveDocumentStatus(parameter) {
var status = $('#hdnDocumentStatus').val();
var userId = $('#hdnCurrentUserId').val();
var uri = "thePage.aspx/theFunction";
const data = {
status: status,
appointmentRequestQueueId: parameter,
userId: userId
};
fetch(uri, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
})
.then(response => response.json())
const theData = response.json();
console.log(theData);
}