3

asp.netでc#を使用してWebアプリケーションを構築していますが、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回のうちの1つが(同じコードで)機能しますが、ほとんどの場合失敗し、次のメッセージが表示されます。

キャッチされていないreferenceEror-seatServiceが定義されていません。

ステータスコードは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 に答える