New help with a ajax, nodejs, jquery form

Here is my function: I am having trouble with my form and getting my postTodo to take my form and post.
function postTodo(title, description) {

    return new Promise((resolve, reject) => {

        try {

            jQuery.ajax({

                url: "http://localhost:3000/todos",

                method: "POST",

                contentType: "application/json",

                data: {

                    title,

                    description

                },

                success: (data, textStatus, jqXHR) => {

                    resolve(data);

                },

                error: (jqXHR, textStatus, error) => {

                    reject({

                        jqXHR,

                        textStatus,

                        error

                    });

                }

            });

        } catch (e) {

            reject(e);

        }

    });

};

function fetchTodos() {

    return new Promise((resolve, reject) => {

        try {

            jQuery.ajax({

                url: "http://localhost:3000/todos",

                success: (data, textStatus, jqXHR) => {

                    resolve(data);

                },

                error: (jqXHR, textStatus, error) => {

                    reject({

                        jqXHR,

                        textStatus,

                        error

                    });

                }

            });

        } catch (e) {

            reject(e);

        }

    });

}; 

// the fetchTodos pulls the tasks
var renderNewTodoForm = () => {

     //   test = JSON.stringify({"title":"test"});

     //   postTodo(test,test)

  

       var html = $("<form id=form1> Title <br> <input type=text value=title name=title> <br> Description <br><input type=text value=test name=description><input type=submit> Add Task</form>")

       f = $("#todoForm").html =html

                                    

       return f[0]

       

    }

// The above is my form

// Below is my new todo for from the form.
var createTodo = () => {

        var title = $("#formTitleElem").val(); 

        var description = $("#formDescElem").val();

         postTodo(title, description).then((todo) => {

           appendNewTodoToList(todo)

         }).catch((err) => {

    

         })

    }

The server is running a post /todos and get /todos

Have you checked the errors in browser’s console window?

Key here was this JSON.stringify({title,description}) Just a syntax error. It is working now.

1 Like