Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have following code in page index.aspx but it is not working. i want to submit name and email to webmethod using Jquery. code is working till it reach to $.post but after that it is not working. Please help

JavaScript
<pre lang="cs">$("#submit").click(function () {
                    var email = $("#email").val();
                    var name = $("#name").val();



                    if (email != '') {
                        if (validateEmail(email)) {

                            $.post('index.aspx/SubscriberSubmit', { name: name, email: email }, function (data) {
                                var newDiv = $(document.createElement('div'));
                                newDiv.html(data);
                                newDiv.dialog({ minHeight: 100, modal: true });
                                $("#email").val('Please Enter Your Email ID');
                                $("#name").val('Please Enter Your Name');
                            });
                        }
                        else {
                            var newDiv = $(document.createElement('div'));
                            newDiv.html('Please Enter a Valid Email ID');
                            newDiv.dialog({ minHeight: 100, modal: true });
                            $("#email").val('');
                        }
                    }

                });


webmethod is in codebehind


C#
<pre lang="cs">[WebMethod]


     public static string SubscriberSubmit(string name, string email)
        {
            return "hello";
        }


Please suggest where i am wrong.............
Posted

What i understood from you post is you want to call one method which is WEBMETHOD i.e SubscriberSubmit().

So my solution, try to use JQuery Ajax call. See following code.

$.ajax(
{
type: "POST",
url: "Index.aspx/Save",
data: JSON.stringify({name:name,email:email}),
contentType: "application/json",
charset: "utf-8",
dataType: "json",
async: true,
cache: false,

success: function (msg) {
alert(msg.d);
},

error: function (x, e) {
alert("The call to the server side failed. " + x.responseText);
}
});


So here once you get succes function call, you can use returned data and use it further with function call.

Try it.
 
Share this answer
 
Comments
aarif moh shaikh 23-Apr-15 6:44am    
agree with you...... :)
maneesh katiyar 23-Apr-15 6:59am    
I have used your code but it returns following error


The call to the server side failed. {"Message":"Invalid web service call, missing value for parameter: \u0027name1\u0027.","StackTrace":" at System.Web.Script.Services.WebServiceMethodData.CallMethod(Object target, IDictionary`2 parameters)\r\n at System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target, IDictionary`2 parameters)\r\n at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"}
maneesh katiyar 23-Apr-15 7:01am    
it,s working sir./

Thank to help.
you are creating a div on fly which is good but where you are creating ???are you appending or prepending to some controls??The error may be in the line "var newDiv = $(document.createElement('div'));"


$.ajax({
type: "POST",
url: "index.aspx/SubscriberSubmit",
data: "{name:'" + name+ "',email:'" + email+ "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: "true",
success: function (response) {
if (response.d) {

var newDiv = $(document.createElement('div'));
newDiv.html(data);
newDiv.dialog({ minHeight: 100, modal: true });
$("#email").val('Please Enter Your Email ID');
$("#name").val('Please Enter Your Name');
alert("your success result goes here");
}
},
error: function (response) {
alert("failure result goes here");
}
});

its better to have a error function declared in your json postbacks.
 
Share this answer
 

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