1

Here is my problem: I have a web service method which returns simple string:

namespace webStockService
{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    // 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 StockService : System.Web.Services.WebService
    {
        [WebMethod]
        public string getChallenge()
        {
            //some codes
            return string;
        }
    }

I want to call this service in javascript:

try
{
   var challenge = webStockService.StockService.getChallenge();
}
catch(e)
{
   alert(e.messsage);
}

I have not got any error message in my call, but when I debug my script the challenge is undefined!!!

I have tested my web service method in asmx fashion and it invokes properly and returns string.

I also have inserted scriptManager tag in my html:

<asp:ScriptManager ID="webStockService" runat="server">
            <Services>
                <asp:ServiceReference Path="StockService.asmx" />               
            </Services>
</asp:ScriptManager> 

any help would be appreciated.

A23149577
  • 2,045
  • 2
  • 40
  • 74

1 Answers1

2

This line must be changed

var challenge = webStockService.StockService.getChallenge();

Please try the following

webStockService.StockService.getChallenge(SuccessCallBack);

function SuccessCallBack(returnedString)
{
  alert(returnedString);
}

You must pass a callback javascript function to execute. The call to webservice is asynchronous in nature and you won't get the intended behavior as you tried in your code. Reading about Ajax will give you better idea. Detailed answer here

Community
  • 1
  • 1
Subin Jacob
  • 4,692
  • 10
  • 37
  • 69