How to resolve “TypeError: the JSON object must be str, bytes or bytearray, not NoneType” error, while working on a flask app with ajax methods?

function myfunction() {

    var firstname = document.getElementById("fname").value;
    var lastname = document.getElementById("lname").value;

    var dict_values = {firstname, lastname} //Pass the javascript variables to a dictionary.
    console.log(dict_values); // Prints the variables to console window, which are in the JSON format
        dataType : "json",
        contentType : "application/json; charset=utf-8",
        data: JSON.stringify(dict_values)});