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

  • 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:
     <script src="MyTester.js" type="text/javascript"></script>
<form id="form1" runat="server">
    <input id="Button1" type="button" value="MyTester" onclick = "callWebService()" />
Here is the Javascript code:
function callWebService()
Here is the codebehind .cs with the Web Service method that we are trying to call:
    public static void FileDeletion()
        int one = 1;
        int two = 2;
        int result = one + two;

Open in new window

  • 2
  • 2
1 Solution
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
    public string SayHello(String inStr) {
        return "Hello : " + inStr + "; Thanks for visiting.";
aspx page
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        <asp:ServiceReference Path="SimpleService.asmx" />
function Button1_onclick() {
ret = SimpleService.SayHello(document.getElementById('Text1').value, OnComplete, OnTimeOut, OnError);

Open in new window

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


Post ref:

metta0_3Author Commented:
Thank you. Just what I was looking for:

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