Click here to Skip to main content
15,880,503 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I am invoking one webservice method using ajax post call. when test with int type, it invokes web service method correctly. But when I tried with hidden value which is string type, it gives internal server error.


//first way --> testing with int type. this invokes the method " getseeestionval.


C#
<script type="text/javascript">

function getval() {

              alert('hi');

      var hidval = document.getElementById('<%=bid.ClientID%>').value;

          alert(hidval);

          $.ajax({

              type:"POST",

              url:"../WebService5.asmx/getseeestionval",

              data:"{'theValue':'1'}",   

              dataType:"json",

              contentType:"application/json; charset=utf-8",

              success:function (data) {

                  OnSuccess(data.d);

              },

              error:function (xhr, status, error) {

                  OnFailure(error);

              }

          });

      }

     function OnSuccess(dateTime) {

 if (dateTime) {

document.getElementById("currentDate").innerHTML = dateTime;

          }

      }

  function OnFailure(error) {

          alert(error);

      }

</script>


aspx page:

calling javascript function when focus on text box

<asp:TextBox ID="txtid" runtat="server" onfocus="getval();">

WebService5.asmx.cs page


C#
[WebMethod(EnableSession = true)]

 public string getseeestionval(int theValue)

        {

     return "s";


         }


//second way --> testing with string type I mean passing hidden value which is string type. this does not invoke

NOTE : I am getting value in hidden value. I tested in alert function. I am getting hidden value.

the method " getseeestionval. It throws internal server error.

C#
<script type="text/javascript">



function getval() {

         var hidval = document.getElementById('<%=bid.ClientID%>').value;

         alert(hidval);

         $.ajax({

        type:"POST",

       url:"../WebService5.asmx/getseeestionval",

       data:"{'theValue':'"+hidval+"'}",   
       dataType:"json",
       contentType:"application/json; charset=utf-8",
       success:function (data) {

       OnSuccess(data.d);

        },


        error:function (xhr, status, error) {

              OnFailure(error);
              }

          });
      }


     function OnSuccess(dateTime) {

 if (dateTime) {

document.getElementById("currentDate").innerHTML = dateTime;

          }


      }

  function OnFailure(error) {

          alert(error);

     }


</script>

aspx page:



calling javascript function when focus on text box



<asp:TextBox ID="txtid" runtat="server" onfocus="getval();">



WebService5.asmx.cs page

[
C#
WebMethod(EnableSession = true)]

 public string getseeestionval(string theValue)

        {

     return "s";

         }



how to solve this problem.
Posted
Updated 12-Aug-14 19:53pm
v2

try this:
data: JSON.stringify({ 'theValue': hidval }),
 
Share this answer
 
Comments
christhuxavier 14-Aug-14 0:14am    
thanks pradeep. your code worked for me.
christhuxavier 14-Aug-14 0:20am    
i passed something like this. it works now.

$.ajax({

type: "POST",
url: "../WebService5.asmx/getseonval",
data: JSON.stringify(theValue),
dataType: "json",
contentType: "application/json; charset=utf-8",

success: function (data) {
OnSuccess(data.d);
},
error: function (xhr, status, error) {
OnFailure(error);
}
try to convert to int like below
C#
data:"{'theValue':parseInt(hidval)}",


Hope this helps
 
Share this answer
 
Please uncomment below lines in webservice, if not already uncommented.
[System.Web.Script.Services.ScriptService]

If that is not solving the problem

Please cross verify web method

[WebMethod]
public string getseeestionval(string theValue)
{
return "s";

}

Also in JavaScript method set cache false.

cache: false,
--------------
I Tested your code, it's working fine for me.
 
Share this answer
 
C#
[WebMethod(EnableSession = true)]

 public static string getseeestionval(int theValue)

        {

     return "s";


         }



use static keyword in function...


I think this will help you
 
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