Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: jQuery JSON WebService
I have code .aspx page:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Params.aspx.cs" Inherits="JQueryAjax.Params" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<style type="text/css">
.loading {  background-image: url('ajax-loader.gif');  background-repeat: no-repeat; }    
 
</style>
    <title></title>
    <script src="jquery.js" type="text/javascript"></script>
   
    <script type="text/javascript">
    function CallService() {     
    $("#lblResult").addClass("loading");  
    $.ajax({    
    type: "POST",
    url: "MyService.asmx/HelloWorld",
    data: "{}",  
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: Success, 
    error: Error     
    });     }   
      function Success(data, status) {      
       $("#lblResult").removeClass("loading"); 
       $("#lblResult").html(data.d);   
         }     
           function Error(request, status, error) 
           {        
            $("#lblResult").removeClass("loading");
            $("#lblResult").html(request.statusText); 
            }
            
     function CallService1() {
            
     $.ajax({   
     type: "POST",
     url: "MyService.asmx/Add",  
     data: "{ 'value1': " + $("#txtValue1").val() + ", 'value2': " + $("#txtValue2").val() + "}",
      contentType: "application/json; charset=utf-8",  
      dataType: "json",     
      success: OnSuccess,
      error: OnError   
      });     } 
      
      function OnSuccess(data, status) {   
      $("#lblResult").html(data.d); 
       } 
       function OnError(request, status, error) { 
       $("#lblResult").html(request.statusText);     
       } 
       
       
         function CallService2() {
            // $("#lblResult").addClass("loading");
     $.ajax({   
     type: "POST",
     url: "MyService.asmx/RecordsTest",  
     data: "{ 'loginid': " + $("#txt_login").val() + ", 'pass': " + $("#txt_pass").val() + "}",
      contentType: "application/json; charset=utf-8",  
      dataType: "json",     
      success: OnSuccess,
      error: OnError   
      });     } 
      
      function OnSuccess(data, status) {   
       $("#lblResult").removeClass("loading"); 
       $("#lblResult").html(data.d); 
       } 
       function Error(request, status, error) 
           {        
            $("#lblResult").removeClass("loading");
            $("#lblResult").html(request.statusText); 
            }
       
       
    </script> 
    
    
 
</head>
<body>
    <form id="form1" runat="server">
        <div>
        
        <div>
        <asp:Label  ID="lbl_date_time" runat="server"></asp:Label>
        </div>
            <table>
                <tbody>
                    <tr>
                        <th>
                            Value 1: 
                        </th>
                        <td>
                            <asp:TextBox ID="txtValue1" runat="server" />
                        </td>
                    </tr>
                    <tr>
                        <th>
                            Value 2: 
                        </th>
                        <td>
                            <asp:TextBox ID="txtValue2" runat="server" />
                        </td>
                    </tr>
                </tbody>
            </table>
        
            <asp:Button ID="btnGo" Text="Hello World" OnClientClick="CallService(); return false;" runat="server" />
            <asp:Button ID="Button1" Text="Add" OnClientClick="CallService1(); return false;" runat="server" />
             
            
            <asp:Label ID="lblResult" Text=" " Width="100%" runat="server" />
            <asp:Label ID="lblRecords" Text=" " Width="100%" runat="server" />
            
            
            <table>
            <tr>
                <td>
                    Login ID</td>
                <td>
                    <asp:TextBox ID="txt_login" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Password</td>
                <td>
                    <asp:TextBox ID="txt_pass" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                     </td>
                <td>
                   <asp:Button ID="Button3" Text="Login check" OnClientClick="CallService2(); return false;" runat="server" />
                </td>
            </tr>
        </table>
        </div>
        
    </form>
</body>
</html>
 
and webservice page like this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.Services;
 
using System.Web.Script.Services;
 

namespace JQueryAjax
{
    /// <summary>
    /// Summary description for MyService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
    [System.Web.Script.Services.ScriptService]
    public class MyService : System.Web.Services.WebService
    {
        [WebMethod]
 
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
 
        public string HelloWorld()
        {
            System.Threading.Thread.Sleep(1000);
            return "Hello World";
        }
 
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
 
        public int Add(int value1, int value2)
        {
            return value1 + value2;
        }
 
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string RecordsTest(string loginid, string pass)
        {
 
            //if (loginid == "Admin")
            //{
            //    return "Exist";
            //}

            return "ssf";
 
            //else
            //{
            //    return "Not Exist";
            //}
            //DataTable dt = new DataTable("MyDataTable");
            //dt.Columns.Add("column1", typeof(System.String));
            //dt.Columns.Add("column2", typeof(System.String));

            //DataRow dr = dt.NewRow();
            //dr["column1"] = "Your Data";
            //dr["column2"] = "Your Data";
            //dt.Rows.Add(dr);

            //dr = dt.NewRow();
            //dr["column1"] = "Your Data";
            //dr["column2"] = "Your Data";
            //dt.Rows.Add(dr);

            //return dt;

        }
    }
}
now when i call webmethod RecordsTest using CallService2
it gives error "Internal server error " ..though rest two web methods working fine..
any help!!!
Posted 29-Dec-12 18:00pm
Edited 29-Dec-12 21:28pm
(no name)498.5K
v2
Comments
Sandeep Mewara at 30-Dec-12 2:29am
   
I don't see anthing named 'CallService2'. What are you talking of?
Sampath Kumar Sathiya at 31-Dec-12 0:14am
   
Hi,
 
Run your site in Google Chrome in that use developer tools. In developer tools use Network Tab. Using that you can find exact problem.
 
Post that detail here.
Sampath Kumar Sathiya at 2-Jan-13 0:28am
   
Hi,
 
Have you tried and found the problem?

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

  Print Answers RSS
0 Maciej Los 295
1 OriginalGriff 273
2 Aajmot Sk 234
3 Marcin Kozub 205
4 Richard MacCutchan 200
0 OriginalGriff 7,903
1 Sergey Alexandrovich Kryukov 7,127
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,820


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 30 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100