Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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>


JavaScript
$(document).ready(function () {
 
    $("#btnRegister").click(function () {
        Register();
    });
 
    function Register() {
            
        $.ajax({


            type: "GET",
            contentType: "application/json; charset=utf-8",
            url: "http://localhost:4780/WebServiceRegistering.asmx/Registering/8",
            contentType: "application/json",
            datatype: "json",
            success: function (data) {
                alert(data);
            },
            error: function (data) {
                alert(" Error aaaaaaa");
            }


 
        });
 
    }
 
});
 
 [WebMethod]
        public string Registering(int Id)
        {
            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. i want when I click #btnRegister return this "{\"registerPachage\":{\"Id\":26}}" from web serice
Posted
Updated 10-Jun-16 17:52pm

Try this,
underline codes are corrected

JavaScript
$.ajax({
                   type: "POST",
                   contentType: "application/json; charset=utf-8",
                   url: "http://localhost:4780/WebServiceRegistering.asmx/Registering",
                   data: "{ 'Id': 8 }",
                   contentIype: "application/json",
                   datatype: "json",
                   success: function (data) {
                       alert(data.d);
                   },
                   error: function (data) {
                       alert(" Error aaaaaaa");
                   }
               });



Make sure the ScriptService attribute is added to the webservice
C#
[System.Web.Script.Services.ScriptService]
   public class WebServiceRegistering : System.Web.Services.WebService
 
Share this answer
 
Using jQuery ajax to call asmx webservice methods | The ASP.NET Forums[^]

Google "call web service ajax" and you'll find plenty more examples.
 
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