I'm buildung a WebApplication in asp.net with c# and i have a problem to call ac# function from the JS.
My code is like this:
in js:
Function doStuff()
{
var service = new seatService.Service1();
service.DoWork(id, onSuccess, null, null);
}
and in the service page is:
[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
}
}
The strange thing is that one of 20 times it actually works (with the same code) but most of the time it fails and I get the message:
uncaught referenceEror - seatService is not defined.
and the status code is 500 Internal Server Error.
Since it sometime working and sometime doesn't - where is my problem?
You want to use PageMethods
for this. Here's some sample code to demonstrate:
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
}
}
From 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());
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.