AJAX调用使用int类型但不使用字符串类型 [英] AJAX call works with int type but not working with string type

查看:195
本文介绍了AJAX调用使用int类型但不使用字符串类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在调用一个使用ajax post调用的webservice方法。当使用int类型进行测试时,它会正确调用Web服务方法。但是当我尝试使用字符串类型的隐藏值时,它会给出内部服务器错误。





//第一种方式 - >用int类型测试。这将调用方法getseeestionval。





 < ; script     type   =  text / javascript >  

function getval(){

alert(' hi');

< span class =code-keyword> var hidval = document.getElementById(' <%= bid .ClientID%>')。 value ;

alert(hidval);

$ .ajax({

类型: POST

url: ../ WebService5.asmx / getseeestionval

数据: {'theValue':'1'}

dataType: json

contentType: application / json; charset = utf-8

成功:函数(数据){

OnSuccess(data.d);

} ,

错误:函数(xhr,状态,错误){

OnFailure(错误);

}

});

}

函数OnSuccess(dateTime){

if ( dateTime){

document.getElementById( currentDate)。innerHTML = dateTime;

}

}

函数OnFailure(错误){

alert(error);

}

< / script >





aspx页面:



专注于文本框时调用javascript函数



< asp:Text Box ID =txtidruntat =serveronfocus =getval();>



WebService5.asmx.cs page





 [WebMethod(EnableSession =  true )] 

public string getseeestionval( int theValue)

{

return s;


}





//第二路 - >用字符串类型测试我的意思是传递隐藏值,这是字符串类型。这不会调用



注意:我在隐藏价值中获得价值。我测试了警报功能。我的隐藏价值。



方法getseeestionval。它会引发内部服务器错误。



< pre lang =c#> < script type = text / javascript >



函数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
成功:函数(数据){

OnSuccess(data.d);

},


错误:函数(xhr,状态,错误){

OnFailure(错误);
}

});
}


函数OnSuccess(dateTime){

if (dateTime){

document.getElementById( currentDate)。innerHTML = dateTime;

}


}

函数OnFailure(错误){

alert(错误);

}


< / script >



aspx页面:







专注于文本框时调用javascript函数







< a sp:TextBox ID =txtidruntat =serveronfocus =getval();>







WebService5.asmx.cs页面



[

 WebMethod(EnableSession =  true )] 

public string getseeestionval( string theValue)

{

return s;

}







如何解决这个问题。

解决方案

.ajax({

type: POST

url: ../ WebService5.asmx / getseeestionval

数据: {'theValue':'1 '}

dataType: json

contentType: application / json; charset = utf-8

成功:函数(数据){

OnSuccess(data.d);

},

错误:函数(xhr,状态,错误){

OnFailure(错误);

}

});

}

函数OnSuccess(dateTime){

if (dateTime){

document.getElementById( currentDate)。innerHTML = dateTime;

}

}

函数OnFailure(错误){

alert(error);

}

< / script < span class =code-keyword>>





aspx页面:



在关注文本框时调用javascript函数



< asp:TextBox ID =txtidruntat =serveronfocus =getval ();>



WebService5.asmx.cs页面





 [WebMethod(EnableSession =  true )] 

public string getseeestionval( int theValue)

{

return s;


}





//第二路 - >用字符串类型测试我的意思是传递隐藏值,这是字符串类型。这不会调用



注意:我在隐藏价值中获得价值。我测试了警报功能。我的隐藏价值。



方法getseeestionval。它会引发内部服务器错误。



< pre lang =c#> < script type = text / javascript >



函数getval(){

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

alert(hidval);


.ajax({

type:< span class =code-string> POST

url: ../ WebService5.asmx / getseeestionval

data: {'theValue':' + hidval + '}
dataType: json
contentType: application / json; charset = utf-8
成功:函数(数据){

OnSuccess(data.d);

},


错误:函数(xhr,状态,错误){

OnFailure(错误);
}

});
}


函数OnSuccess(dateTime){

if (dateTime){

document.getElementById( currentDate)。innerHTML = dateTime;

}


}

函数OnFailure(错误){

alert(错误);

}


< / script >



aspx页面:







专注于文本框时调用javascript函数







< a sp:TextBox ID =txtidruntat =serveronfocus =getval();>







WebService5.asmx.cs页面



[

 WebMethod(EnableSession =  true )] 

public string getseeestionval( string theValue)

{

return s;

}







如何解决这个问题。


试试这个:

数据:JSON.stringify({'theValue':hidval}),


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.


<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


[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.

<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

[

WebMethod(EnableSession = true)]

 public string getseeestionval(string theValue)

        {

     return "s";

         }




how to solve this problem.

解决方案

.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


[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.

<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

[

WebMethod(EnableSession = true)]

 public string getseeestionval(string theValue)

        {

     return "s";

         }




how to solve this problem.


try this:
data: JSON.stringify({ 'theValue': hidval }),


这篇关于AJAX调用使用int类型但不使用字符串类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆