2017-08-06 69 views
1

我在使用c#在asp.net中創建網站的實時服務器上從ajax提交數據時出現錯誤。當在asp.net的實時服務器中從AJAX提交數據時發生500內部服務器錯誤

的錯誤是 「500(內部服務器錯誤)」

這裏是我的代碼

<script> 
    $(function() { 
     $(document).on("click", "#submit_mail", function (e) { 
      if (validateform() == false) { 
       e.preventDefault(); 
      } 
      else { 
       $.ajax({ 
        type: "POST", 
        url: "contact.aspx/SendMail", 

        data: JSON.stringify({ name: $('#txt_name').val(), email: $('#txt_emailID').val(), subject: $('#txt_subject').val(), message: $('#txt_content').val() }), 
        contentType: "application/json; charset=utf-8", 
        dataType: "json", 
        success: function() { 
         alert("Email Send Successfully, we will contact you successfully...!!!"); 
         location.reload(); 
        }, 
        failure: function() { 
         alert("There is some problem in server to contact with us....Please contact with contact number...!!!"); 
        } 
       }); 
      } 
     }); 
    }); 
</script> 

現在,這是代碼。這段代碼在本地服務器上工作正常。在活動服務器中,此代碼不起作用。

當我調試的開發工具這AJAX它表明,它調用了ajax線即 $.ajax({和這就是爲什麼在Web服務器的方法是服務器端的代碼,它會之間的調試是不會結束它)}; 等空數據並顯示500內部服務器錯誤。

這裏是服務器端代碼

[WebMethod] 
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
    public static void SendMail(string name, string email, string subject, string message) 
    { 
     //Thread.Sleep(10000); 
     // Gmail Address from where you send the mail 
     var fromAddress = "[email protected]"; 
     // any address where the email will be sending 
     var toAddress = email.Trim(); 
     //Password of your gmail address 
     const string fromPassword = "xxxxxxxxxxx"; 
     // Passing the values and make a email formate to display 
     string sub = subject.Trim(); 
     string body = "From: " + name.Trim() + "\n"; 
     body += "Email: " + email.Trim() + "\n"; 
     body += "Subject: " + subject.Trim() + "\n"; 
     body += "Message: \n" + message.Trim() + "\n"; 
     // smtp settings 
     var smtp = new System.Net.Mail.SmtpClient(); 
     { 
      smtp.Host = "smtp.gmail.com"; 
      smtp.Port = 587; 
      smtp.EnableSsl = true; 
      smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; 
      smtp.Credentials = new NetworkCredential(fromAddress, fromPassword); 
      smtp.Timeout = 20000; 
     } 
     // Passing values to smtp object 
     smtp.Send(fromAddress, toAddress, sub, body); 
    } 

所以,在短期我的問題是,爲什麼在AJAX方法調試未服用的URL,數據和AJAX的其他內容。因爲對於我的想法,當數據從AJAX獲取網址,數據和其他內容時,這個錯誤即將到來。

+0

show me c#code。 –

+0

我已更新數據,請檢查它。但對我的想法有一個Ajax問題,因爲它不是內容的網址,數據和其他內容,並從該Web服務器方法爲空。因此得到這個錯誤。 –

+0

似乎url問題的URL我們不能找到方法你打電話你可以在這裏粘貼你的控制檯錯誤嗎? – Aamir

回答

0

////Send Email on button click 
 
function SendEmail(url) { 
 

 
    obj = {}; 
 
    obj.Name = $("#txtName").val(); 
 
    obj.Email = $("#txtEmail").val(); 
 
    obj.MobileNo = $("#txtMobileNo").val(); 
 

 
    obj.Subject = ($("#txtSubject").val() != '') ? $("#txtSubject").val() : ''; 
 
    obj.Message = ($("#txtMessage").val() != '') ? $("#txtMessage").val() : ''; 
 

 
    $.ajax({ 
 
     type: "POST", 
 
     url: url, 
 
     data: JSON.stringify(obj), 
 
     contentType: "application/json; charset=utf-8", 
 
     dataType: "json", 
 
     success: OnSuccess, 
 
     failure: function (response) { 
 
      alert(response.d); 
 
     } 
 
    }); 
 

 
    function OnSuccess(response) { 
 
     $.each(response.d, function() { 
 

 
      var Msg; 
 
      var Result; 
 
      Msg = this['Text']; 
 
      Result = this['Value']; 
 

 
      if (Result > 0) { 
 
       Clear(); 
 
       $("#AlertClass").attr("style", "display: block") 
 
       $('#AlertClass').addClass('alert alert-success text-center'); 
 
       $('#lblResult').append('<span><img src="assets/images/Serve.png" style=" height: 40px;" /> &nbsp;&nbsp;' + Msg + '</span>'); 
 

 
      } else { 
 
       $("#AlertClass").attr("style", "display: block") 
 
       $('#AlertClass').addClass('alert alert-danger text-center'); 
 
       $('#lblResult').append('<span>"' + Msg + '"</span>'); 
 
      } 
 
     }); 
 
    } 
 
}
 [WebMethod] 
 
     public static List<ListItem> SendEmail(string Name, string Email, string MobileNo, string Subject, string Message) 
 
     { 
 
      
 
      int Return = 0; 
 
      try 
 
      { 
 
      } 
 
      catch (Exception) 
 
      { 
 
       ReturnMessage.Add(new ListItem 
 
       { 
 
        Value = "0", 
 
        Text = "There is an error try again after some time!" 
 
       }); 
 
      } 
 

 
      return ReturnMessage; 
 
     }

試試這個。

+0

通過在本地服務器中嘗試您的代碼只顯示500(內部服務器錯誤) –

相關問題