Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 584
  • Last Modified:

Trying to call a C# Web service method from Javascript. Cannot get it to work.

I am trying to call a Web Service method from javscript function. I have managed to do it before, this time it will not work. i cannot remember if I need to make modifications to the web.config file. Please any assistance would be greatly appreciated. Code example is below.
Here is the .aspx code:
<head>
     <script src="MyTester.js" type="text/javascript"></script>
</head>
<form id="form1" runat="server">
    <input id="Button1" type="button" value="MyTester" onclick = "callWebService()" />
 </form>
 
Here is the Javascript code:
 
function callWebService()
{
    PageMethods.FileDeletion();    
}
 
Here is the codebehind .cs with the Web Service method that we are trying to call:
 
[System.Web.Services.WebMethod]
    public static void FileDeletion()
    {
        int one = 1;
        int two = 2;
        int result = one + two;
    }

Open in new window

0
metta0_3
Asked:
metta0_3
  • 2
  • 2
1 Solution
 
jabcocoCommented:
PageMethods are use when you have a "PageMethods" in your aspx code-behind page. If you want to access a web-services you need to add the asmx ref to the scriptmanager.

Take a look at these video tutorial from asp.net
http://www.asp.net/learn/ajax-videos/video-82.aspx
webservices
------------------
    [WebMethod]
    public string SayHello(String inStr) {
        return "Hello : " + inStr + "; Thanks for visiting.";
    }
 
 
aspx page
----------------
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        <Services>
        <asp:ServiceReference Path="SimpleService.asmx" />
        </Services>
        </asp:ScriptManager>
 
 
function Button1_onclick() {
ret = SimpleService.SayHello(document.getElementById('Text1').value, OnComplete, OnTimeOut, OnError);
return(true);
}

Open in new window

0
 
metta0_3Author Commented:
Thanks for your advice. That is very useful. All I really want to do is call an asp.net method from Javascript. There must be a way to do this without the need to create web services. It just seems like alot for something so small. Creating web services just to call one asp.net method in code behind from Javascript.

Any idea on alternative approach.
0
 
jabcocoCommented:
I've just answer a similar question few days ago.

You will need to use the "PageMethod"
Check this out. This will explain how to call a "asp.net method" from your javascript page.

http://weblogs.asp.net/sohailsayed/archive/2008/02/23/calling-methods-in-a-codebehind-function-pagemethods-from-client-side-using-ajax-net.aspx


Post ref:
http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_24123347.html

0
 
metta0_3Author Commented:
Thank you. Just what I was looking for:
EnablePageMethods="true"
Regards...
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now