3

我正在用 c# 在 asp.net 中构建一个 WebApplication,我在从 JS 调用 ac# 函数时遇到问题。

我的代码是这样的:

在js中:

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

在服务页面中是:

[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
    }
}   

奇怪的是,它实际上工作了 20 次(使用相同的代码),但大多数时候它失败了,我得到了消息:

未捕获的referenceError - 未定义座位服务。

状态码是500 Internal Server Error.

因为它有时工作,有时不工作 - 我的问题在哪里?

4

1 回答 1

2

你想用PageMethods这个。下面是一些示例代码来演示:

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

来自 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());
}
于 2013-03-09T23:04:23.120 回答