无法从JavaScript调用Asp.net方法 [英] Failed to call Asp.net method from Javascript

查看:130
本文介绍了无法从JavaScript调用Asp.net方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从JavaScript调用Asp.Net功能。我有一个样品被调用Asp.net MVC功能可按这样的。

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

    <! - 节省使用会话值,使唯一的文件名的wav文件到本地文件夹,名为recodings  - >
}
    功能SETU precorder(){
        Wami.setup({
            ID:瓦米
            onReady:setupGUI
        });
    }

    功能setupGUI(){
        VAR GUI =新Wami.GUI({
            ID:瓦米
            recordUrl:HTTP://本地主机:5296 /家居/保存,
            playUrl:@payUrl
        });

        gui.setPlayEnabled(假);
    }
 

下面是从code确切的通话

  recordUrl:HTTP://本地主机:5296 /家居/保存,
 

在HomeController中有一个方法保存它正在这里称为

 公众的ActionResult保存()
        {
            Request.SaveAs(使用Server.Mappath(/录像/录音123.wav),FALSE);
            返回JSON(新{成功= TRUE},JsonRequestBehavior.AllowGet);
        }
 

我想在Asp.Net确切的事情,但我不能够找到解决办法,如果有任何人能帮助我,我将感谢你,这是唯一的部分留在我的项目,我只需要保存音频。我做这样的

 <脚本>
    功能SETU precorder(){
        Wami.setup({
            ID:瓦米
            onReady:setupGUI
        });
    }

    功能setupGUI(){
        VAR GUI =新Wami.GUI({
            ID:瓦米
            recordUrl:Default.aspx的/保存,
            playUrl:/recordings/recording-123.wav
        });

        gui.setPlayEnabled(假);
    }
< / SCRIPT>
 

我有一个WebForm的Default.aspx有方法保存

  recordUrl:Default.aspx的/保存,
 

这是确切的default.aspx.cs方法。我曾尝试[HTTPGET]和[httpPost]既没有什么工作对我来说。

  [HTTPGET]
[System.Web.Services.WebMethod]
    公共无效保存()
        {
            Request.SaveAs(使用Server.Mappath(/录像/录音123.wav),FALSE);
            返回JSON(新{成功= TRUE},JsonRequestBehavior.AllowGet);
        }
 

解决方案

我觉得你的保存方法需要被标记,因为它的一个WebMethod静态。只有这样,你应该要能够从JS调用它

  [System.Web.Services.WebMethod]
  公共静态无效保存()
    {
        Request.SaveAs(使用Server.Mappath(/录像/录音123.wav),FALSE);
        返回JSON(新{成功= TRUE},JsonRequestBehavior.AllowGet);
    }
 

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);
    }

这篇关于无法从JavaScript调用Asp.net方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆