0

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);
        }
Lucas Hendren
  • 2,786
  • 2
  • 18
  • 33
  • 2
    What is the actual JSON being returned? – evolutionxbox Jun 02 '22 at 23:54
  • @evolutionxbox: I never get the JSON back. It never hits the success block. It errors out before then. – Mike Sterling Jun 02 '22 at 23:56
  • 1
    Try using `fetch` and seeing what the response is, without jquery trying to parse it – evolutionxbox Jun 02 '22 at 23:57
  • JSON keys need to be wrapped double quotes (`"`), so it should be `data: "{\\"status\\":...}"`. You don't have to do that though. Just do `data: JSON.stringify({ status, appointmentRequestQueueId: parameter, userId, })`. – code Jun 03 '22 at 00:09
  • @evolutionxbox: I took a stab at re-writing it using fetch but I'm not sure I coded it correctly. – Mike Sterling Jun 03 '22 at 00:40
  • @code: It doesn't like the colon (:): "Message":" is not a valid value for Int32.", – Mike Sterling Jun 03 '22 at 00:46
  • Never manually create your own JSON strings. Always use `JSON.stringify()`. Your `fetch` syntax is ok but you'll need something like `.then(theData => { console.log(theData) })` at the end since it's all async – Phil Jun 03 '22 at 01:14

0 Answers0