I am trying to utilize AJAX to call a method in my CS page. Below is the design code I am using:
<!-- Name -->
<input type="text" name="name" id="name" required="required" class="form" placeholder="Name" />
<!-- Email -->
<input type="email" name="mail" id="mail" required="required" class="form" placeholder="Email" />
<!-- Subject -->
<input type="text" name="subject" id="subject" required="required" class="form" placeholder="Subject" />
<!-- Message -->
<textarea name="message" id="message" class="form textarea" placeholder="Message"></textarea>
<!-- Send Button -->
<button type="button" id="submit" name="submit" class="form-btn semibold">Send Message</button>
Here is the ajax implementation:
$(document).on("click", "#submit", function (e) {
$.ajax({
type: "POST",
url: "OakscrollWebService.cs/SendMail",
dataType: "json",
data: JSON.stringify({ name: $('#name').val(), email: $('#mail').val(), subject: $('#subject').val(), message: $('#message').val() }),
contentType: "application/json; charset=utf-8",
success: function (data) {
alert(data.d);
},
failure: function (data) {
alert("something went wrong");
//console.log(msg);
}
});
});
I have also added an asmx page (web service) with a reference call to the CS file in the App_Code folder. Here is the code:
<%@ WebService Language="C#" CodeBehind="~/App_Code/OakscrollWebService.cs" Class="OakscrollWebService" %>
The CS file contains the method I am trying to call using AJAX. Here is the method code:
[WebMethod]
public static void SendMail(string name, string email, string subject, string message)
{
// Code for sending email
}
Despite implementing the ajax call, I am facing issues like "403 forbidden" and "500 server not found", preventing me from successfully calling the SendMail method.