簡體   English   中英

從javascript dosent work調用ac#函數

[英]calling a c# function from javascript dosent work

我用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次之一(使用相同的代碼),但大部分時間它失敗了,我收到了消息:

未捕獲的referenceEror - 未定義seatService。

狀態代碼為500 Internal Server Error.

因為它有時工作,有時不工作 - 我的問題在哪里?

您想要使用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());
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM