In my AJAX enabled website, call web service by javascript.
Web Service:
<WebService(Namespace:="http://tempuri.org/")> _<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _<ScriptService()> _Public Class SimpleServiceInherits System.Web.Services.WebService<WebMethod()> _Public Function Addup(ByRef int1As Integer,ByRef int2As Integer)As Booleanint1 = int1 + int2Return TrueEnd FunctionEnd Class
Javascript:
var num1=1, num2=2;function Button1_onclick() {HangmanService.Addup(num1, num2, OnComplete);}function OnComplete(ret){if (ret == true){alert(num1);// should popup "3"}}
Actually, I got error of "System.InvalidOperationException -- Can't convert object of type "System.Int32" to type "System.Int32&" instead of "3". So do I have to get number from return value?
Hi,
the reference to num1 on server side has nothing to do with the reference to num1 on client side, they are two separate variables. Therefore Addup must return a number.
Try this,
<WebService(Namespace:="http://tempuri.org/")> _<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _<ScriptService()> _Public Class SimpleServiceInherits System.Web.Services.WebService<WebMethod()> _Public Function Addup(ByRef int1As Integer,ByRef int2As Integer)As Stringint1 = int1 + int2Return int1.toString()End FunctionEnd Class
Javascript:
var num1=1, num2=2;function Button1_onclick() {HangmanService.Addup(num1, num2, OnComplete);}function OnComplete(result){alert(result); // should popup "3"}
WS
Thank you guys' reply.
The problem is not about return value but javascript even can't call the web method. Maybe in javascript a variable can't be reference parameter.
Ah, gotcha,
It's a little bit odd how you call your webservice function. Should it be ...
SimpleService.Addup(num1, num2, OnComplete); ......???
WS
I believe you should return the number in the return value if it's possible.
AJAX serializes input parameters and return values. I think that using ByRef is not going to work.
If you need to get those two values back (bool and int), you should consider returning a different type.
Hope this helps,
MaĆra
Thanks for your reply. I just wanna know if it works. Anyway, I know it.
No comments:
Post a Comment