简体   繁体   中英

Failed to call Asp.net method from Javascript

I want to call Asp.Net function from Javascript. I have a sample which is calling Asp.net MVC funciton like that.

@{
    var payUrl = "/recordings/recording-123.wav";

    <!-- saves the wav file to local folder called recodings using a session value to make 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 exact calling from the code

recordUrl: "http://localhost:5296/home/Save",

The HomeController has a method Save which is being called here

public ActionResult Save()
        {
            Request.SaveAs(Server.MapPath("/recordings/recording-123.wav"), false);
            return Json(new { Success = true }, JsonRequestBehavior.AllowGet);
        }

I want the exact thing in Asp.Net but i am not able to find the solution if anyone out there can help me out i would be thankful to you this is the only part left in my project i simply need to save the audio. I am doing like this

<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 which has method save

recordUrl: "Default.aspx/Save",

This is the exact default.aspx.cs method. I have tried [HttpGet] and [httpPost] both nothing is 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);
        }

I think your save method needs to be marked as static since its a webmethod. Only then you should to be able to call it from JS

  [System.Web.Services.WebMethod]
  public static void Save()
    {
        Request.SaveAs(Server.MapPath("/recordings/recording-123.wav"), false);
        return Json(new { Success = true }, JsonRequestBehavior.AllowGet);
    }

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM