Click here to Skip to main content
15,909,051 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
when i Call below javascript function. it gives Reference Error : "data is not defined"
so what mistak i have done.. i cant understand...so please help me..

What I have tried:

C#
function SaveRecords() {
            var fname = $('#txtFirstName').val();
            var mname = $('#txtMiddelName').val();
            var lname = $('#txtLastName').val();

            var UserRegistration = {};
            UserRegistration.FirstName = fname;
            UserRegistration.MiddelName = mname;
            UserRegistration.LastName = lname;

            $.ajax({
                type: "POST",
                url: "Login.aspx/SaveRecords",
                data: JSON.stringify({ 'UserRegistration': UserRegistration }),
                contentType: "application/json; charset=utf-8",
                datatype: "json",
                success: function (data) {
                    var result = data.d;
                    if (result == 1) {
                        alert("Record Saved Successfully...!");
                        window.location.reload();
                    }
                    else {
                        alert("oops!! Something Went Wrong..");
                    }
                }
            })
        }
Posted
Updated 7-Sep-19 0:02am
v3
Comments
zamanipour 7-Sep-19 7:28am    
what i can suggest is to check the incoming data first: console.log(data); it might be 'undefined' or 'null'. if so, what exactly your back-end passed. go check your back-end return value. also check for 'cache: false' and if all was not working, use .done() as mentioned in https://api.jquery.com/jquery.ajax/
ZurdoDev 9-Sep-19 9:01am    
First, figure out which line of code is causing the error. You have 2 different data variables so you need to know which one is causing the error.

If it's the first data then that likely means that the code
JSON.stringify({ 'UserRegistration': UserRegistration })
is generating an error or a null. Debug it and find out.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900