3

I'm buildung a WebApplication in asp.net with c# and i have a problem to call a c# function from the JS.

My code is like this:

in js:

Function doStuff()
{
    var service = new seatService.Service1();
    service.DoWork(id, onSuccess, null, null);
}

and in the service page is:

[ServiceContract(Namespace = "seatService")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1 
{
    [OperationContract]
    public static  void DoWork(string id)
    {
      //here there is a function that calls the DB
    }
}   

The strange thing is that one of 20 times it actually works (with the same code) but most of the time it fails and I get the message:

uncaught referenceEror - seatService is not defined.

and the status code is 500 Internal Server Error.

Since it sometime working and sometime doesn't - where is my problem?

4
  • There isn't enough information in the error you provide. Error 500 could be many options. You need to debug your page using developer tools and capture the response body that comes from the server using network capture, there you will see a clearer error info. See here example how to do it in Internet explorer: msdn.microsoft.com/en-us/library/gg130952(v=vs.85).aspx Commented Mar 9, 2013 at 22:31
  • 1
    Function in your JavaScript code should be function (lowercase) Commented Mar 9, 2013 at 22:47
  • I did that but when i look for the response body it says that there is no data to show. the response header is:Key Value Response HTTP/1.1 500 Internal Server Error Server ASP.NET Development Server/10.0.0.0 Date Sat, 09 Mar 2013 22:50:06 GMT X-AspNet-Version 4.0.30319 Cache-Control private Content-Type text/html; charset=utf-8 Content-Length 9527 Connection Close Commented Mar 9, 2013 at 22:52
  • It is in lowercase. It starts with capital f only here by mistake. Commented Mar 9, 2013 at 22:58

1 Answer 1

2

You want to use PageMethods for this. Here's some sample code to demonstrate:

public partial class ContactUs : System.Web.UI.Page
{
   [WebMethod]
   public static void SendForm(string name, string email, string message)
   {
       if (string.IsNullOrEmpty(name))
           throw new Exception("You must supply a name.");    
       if (string.IsNullOrEmpty(email))
           throw new Exception("You must supply an email address.");   
       if (string.IsNullOrEmpty(message))
           throw new Exception("Please provide a message to send.");

       //call your web service here, or do whatever you wanted to do
   }
}

From JavaScript:

SendForm = function() {
   var name = $get("NameTextBox").value;
   var email = $get("EmailTextBox").value;
   var message = $get("MessageTextBox").value;

   PageMethods.SendForm(name, email, message, OnSucceeded, OnFailed);
}    
OnSucceeded = function() {
   $get("ContactFieldset").innerHTML = "<p>Thank you!</p>";
}    
OnFailed = function(error) {
   alert(error.get_message());
}
Sign up to request clarification or add additional context in comments.

2 Comments

I have tried it but i got the message: "Microsoft JScript runtime error: 'PageMethods' is undefined"... the class ContactUs is a the c# class of asp that runs the js or it is the Service class?
Ok. it works. i added the EnablePageMethods="true" in the scriptManager in the asp page. thanks for the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.