Is it possible to call an Asp.Net function from Javascript? I have a code sample that calls an Asp.net MVC function in this way:
@{
var payUrl = "/recordings/recording-123.wav";
<!-- saves the wav file to local folder called recordings using a session value to create unique file names -->
}
function setupRecorder() {
Wami.setup({
id: "wami",
onReady: setupGUI
});
}
function setupGUI() {
var gui = new Wami.GUI({
id: "wami",
recordUrl: "http://localhost:5296/home/Save",
playUrl: "@payUrl"
});
gui.setPlayEnabled(false);
}
Here is the specific code segment being referenced:
recordUrl: "http://localhost:5296/home/Save",
The HomeController contains a method named Save that is invoked here:
public ActionResult Save()
{
Request.SaveAs(Server.MapPath("/recordings/recording-123.wav"), false);
return Json(new { Success = true }, JsonRequestBehavior.AllowGet);
}
I am attempting to achieve the same functionality in Asp.Net but struggling to find a solution. If anyone can offer assistance, I would greatly appreciate it as this is a crucial aspect of my project for saving audio data. Here is my attempt:
<script>
function setupRecorder() {
Wami.setup({
id: "wami",
onReady: setupGUI
});
}
function setupGUI() {
var gui = new Wami.GUI({
id: "wami",
recordUrl: "Default.aspx/Save",
playUrl: "/recordings/recording-123.wav"
});
gui.setPlayEnabled(false);
}
</script>
I have a webform Default.aspx with a method named save:
recordUrl: "Default.aspx/Save",
This is the implementation of the method in default.aspx.cs. I have tried using [HttpGet] and [HttpPost] tags, but nothing seems to be working for me.
[HttpGet]
[System.Web.Services.WebMethod]
public void Save()
{
Request.SaveAs(Server.MapPath("/recordings/recording-123.wav"), false);
return Json(new { Success = true }, JsonRequestBehavior.AllowGet);
}