web services and javascript don't work
-
Thursday, May 03, 2012 10:58 PM
I need to call a web service from html page using javascript and simply it doesn't work. I did tried the example from http://msdn.microsoft.com/en-us/library/ie/ms531034%28v=vs.85%29.aspx. As you can see I had substituted the values for my specific case. This is a very simple example, I don't understand why it wouldn't work. I did upoload the webservice.htc file to the server where the html code resides as per msdn suggestion.
<html> <head> <script language="JavaScript"> var iCallID; function init() { service.useService("http://155.16.25.13/services/sccsystem?WSDL","Connect"); iCallID = service.Connect.callService("logonUser","test","test"); } function onWSresult() { if((event.result.error)&&(iCallID==event.result.id)) { var xfaultcode = event.result.errorDetail.code; var xfaultstring = event.result.errorDetail.string; var xfaultsoap = event.result.errorDetail.raw; // Add code to output error information here
alert(" Horror! "); } else { alert("The method returned the result : " + event.result.value); } } </script> </head> <body onload="init()"> <div id="service" style="behavior:url(webservice.htc)" onresult="onWSresult()"> </div> </body> </html>
All Replies
-
Wednesday, May 09, 2012 2:17 AMModerator
Hi, you can try to achieve this with jQuery Ajax, for example:
Default.aspx:
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script src="http://code.jquery.com/jquery-latest.js" type="text/javascript"></script> <script type="text/javascript"> $(function () { $('#Button1').click(function () { $.ajax({ url: "MyMath.asmx/AddInt", type: "post", data: "{'a':" + $("#TextBox1").val() + ",'b':" + $("#TextBox2").val() + "}", dataType: "json", contentType: "application/json;charset=utf-8", success: function (data) { alert(data.d); }, error: function () { } }); }); }); </script> <style type="text/css"> .style1 { width: 100%; } .style2 { width: 62px; } </style> </head> <body> <form id="form1" runat="server"> <div> <table class="style1"> <tr> <td class="style2"> a:</td> <td> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> </td> </tr> <tr> <td class="style2"> b:</td> <td> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> </td> </tr> </table> <input id="Button1" type="button" value="button" /> </div> </form> </body> </html>MyMath.asmx.cs:
[System.Web.Script.Services.ScriptService] public class MyMath : System.Web.Services.WebService { [WebMethod] public int AddInt(int a, int b) { return a + b; } }For more detail information, please refer to the following link:
http://api.jquery.com/jQuery.ajax/ -
Wednesday, May 09, 2012 5:22 AM
@Allen
please don't feed the sea gulls.... always include a valid DTD with your sample code... thx.
Rob^_^


