Error Internal Server 500 xhr.send (options.hasContent && options.data || null);

0

Good morning.

I have a Javascript process that makes a POST call to a controller, and I'm getting Internal Server 500 error, and in jquery this line marks me

 xhr.send( options.hasContent && options.data || null );

This only happens if I return empty data from the controller. If you perform the correct process does not leave that error. The problem is that it is a continuous call to that controller and the console fills me with error messages, although the operation of the program is correct.

I leave you the javascript code and the controller.

function getMessagesUser(e) {

    //Una vez cargada la página pinto el historial de mensajes de esa conversación.
    var values = {
        "userID": "11",
        "appID": "22"
    };

    $.ajax({
        url: urlControl + "/GetMessages",
        dataType: "json",
        type: "POST",
        data: values,
        success: function (msgsUser) {
            if (msgsUser != null && msgsUser != "") {
                for (x = 0; x < msgsUser.length; x++) {
                  //Realizo la acción
                }
            }
        },
        failure: function (errMsg) {
            console.log(errMsg);
        } 
    });


    var timer = setTimeout(getMessagesUser, 5000);
}

The controller

[HttpPost("GetMessages")]
    public async Task<IActionResult> GetMessages([FromServices] IChat AC)
    {

        //Busco mensajes pendientes para ese usuario
        List<MessageUser> listMsg = await AC.GetMessagesPending();
        var resultMsg = listMsg;//Hago copia de la lista que se va a enviar


        if (resultMsg != null)
        {
            return Json(resultMsg);

        }

        return Json(String.Empty);
    }

If there is data, it returns it correctly and it looks good on the screen, but if there are no messages to return, for each call made by the javascript, that message appears.

Can you help me please?

    
asked by Sergio 10.10.2017 в 16:06
source

1 answer

1

Try modifying your driver to, instead of sending an empty string, send an empty object :

return Json(new { }, JsonRequestBehavior.AllowGet);

The reason may be that the call waits for a JSON (in your script you specify it in dataType: "json" ) and when returning from the controller an empty string, what it receives is an invalid JSON ( "" ) when it should receive a Json empty ( {} ).

Speaking of the specific error that is giving you is due to this:

  

When processing the server response, xhr (the library that handles asynchronous calls) prepares a options object with, among other fields, a bool called hasContent and a data object that contains the response from the server.

When an error occurred while parsing the JSON within data , the latter remains as undefined or null . As hasContent is true, when entering the comparison && will try to work with data but being this not defined generates the error. If hasContent is false (as would happen with an empty object) it simply goes to the other side of the operator || and would continue with null without giving any error.

    
answered by 10.10.2017 / 16:48
source