Click here to Skip to main content
16,020,811 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
HTML
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="jquery-1.12.0.min.js"></script>
    <script src="Lash.js"></script>
</head>
<body>
   first name <input id="txtFirstname" type="text" /><br />
   last name  <input id="txtLastname" type="text" /><br />
   E-mail     <input id="txtEmail" type="text" /><br />
   password   <input id="txtPassword" type="password" /><br />
    <input id="btnRegister" type="button" value="Register" /><br />
</body>
</html>

/// <reference path="jquery-1.12.0.min.js" />

$(document).ready(function () {

    $("#btnRegister").click(function () {
        Register();
    });

    function Register() {

        var obj = { Firstname: $("#txtFirstname").val(), Lastname: $("#txtLastname").val(), Email: $("#txtEmail").val(), Password: $("#txtPassword").val() };

            $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "http://localhost:7910/WebService.asmx/Registering",
            data:JSON.stringify(obj),
            datatype: "json",
            success: function (data) {
                alert("Successfully register");
            },
                error: function (data) {
                alert("error");
            }

        });

    }

});

 [WebMethod]
        public string Registering(string user3gmail, string pass3, string name3, string nickname3, string birth)
        {
            return "{\"registerPachage\":{\"Id\":26}}";
        }


What I have tried:

I'm a beginner in Ajax Jquery and I'm doing exercise to improve my knowledge about it. My problem is when I click #btnRegister it's print error not Successfully message . I think there's a problem in the parameters I passed on the ajax but I don't know what is it.
Posted
Updated 27-May-16 18:46pm

1 solution

Correct these mistakes

1) To Enable the WebService method to be called from Ajax, add the ScriptService attribute as
C#
 [System.Web.Script.Services.ScriptService]
 public class WebService1 : System.Web.Services.WebService
 {

2)
The Parameter names are not matching in the Ajax data and in the CS Method, the names should be same, else the data wont get mapped properly
C#
[WebMethod]
      public string Registering(string Firstname, string Lastname, string Email, string Password )
      {
          return "{\"registerPachage\":{\"Id\":26}}";
      }


3) Since you are using ASP.Net, if the asmx file in the same project then its not necessry to specify the entire url including the port number in Ajax url, if so then you will have to change it each and every time when the port number gets changed. instead you shall write only the service name and the MethodName as
JavaScript
url: "WebService1.asmx/Registering",

if you using only plain html page then you can write the full url including the port number.

Note: Dont use the name WebService, since it is already used in asp.net framework and also the web service name is inherited from it, so give some meaningful name to it.
 
Share this answer
 
Comments
Osama Elsayed 28-May-16 10:29am    
thank you it work well ^_^
Karthik_Mahalingam 28-May-16 10:30am    
Welcome :)
Osama Elsayed 28-May-16 10:32am    
can i connect with you ?
Karthik_Mahalingam 28-May-16 10:33am    
Via Codeproject :)
Osama Elsayed 28-May-16 10:37am    
can via Facebook ?

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