【问题标题】:Call controller method from javascript从 javascript 调用控制器方法
【发布时间】:2015-02-22 07:20:38
【问题描述】:

我想从jquery 调用中调用控制器方法。这是我的code。

<script>
function showImage(id) {
    $.get('/Canvas/getPath?id=' + id, function (data) {
        if (data != '') {
            var ext = /[^.]+$/.exec(data);
            var filename = data;
            if(ext == 'tif')
            {                    
                //window.open('/DocViewer/DocViewer.aspx?FilePath=' + data, 'WindowPopup', 'height=auto,width=auto,status=no,toolbar=no,resizable=yes,scrollbars=yes');
                window.open('/DocViewer/DocViewer.aspx', 'WindowPopup', 'height=auto,width=auto,status=no,toolbar=no,resizable=yes,scrollbars=yes');
            }
            else if(ext == 'xml'){
                $.ajax('/Canvas/getXml?filePath=' + filename, function (xmldata) {
                    alert(xmldata);
                    window.open(xmldata, "WindowPopup", 'height=auto,width=auto,status=no,toolbar=no,resizable=yes,scrollbars=yes');
                }); 
            }
        }
        else {
            alert('Document does not exists. Please make sure the document is on location.');
        }
    });
};
</script>

编辑

这是我的控制器方法

    public JsonResult getXml(string filePath)
    {
        if (System.IO.File.Exists(filePath))
        {
            string xmlString = System.IO.File.ReadAllText(filePath);
            return Json(xmlString, JsonRequestBehavior.AllowGet); ;
        }
        else
        {
            return Json("", JsonRequestBehavior.AllowGet);
        }
    }

这是我想要的,如果ext 如果是xml,那么我想调用controller 的method。在上面的代码中,它给了我new { filePath = filename } 文件名不存在的错误。基本上我试图在浏览器中打开 xml 文件。在数据中,我收到了 xml 文件的 filePath。如何调用控制器方法或在浏览器中打开 xml 文件?

【问题讨论】:

  • 您正在从本地系统打开一个文件。除非它来自上传的文件,否则在浏览器安全实践中是不允许的。如果我们能做到这一点,那么我们所有的开发人员都可以在本地访问用户文件。
  • 这就是为什么我试图调用返回 xml 字符串的控制器方法。以便我可以在浏览器中显示它
  • 更好的方法是解析您的 XML 并将内容作为 XML 或 JSON 返回给客户端,而不是文件本身。
  • 是的,我想实现。但我不能在 else 块中调用控制器方法
  • @agentpx 如何在else if 块中调用控制器方法?

标签: javascript c# jquery xml asp.net-mvc-4


【解决方案1】:

大家好,我的问题已经解决了。这是我完整的script

<script>
function showImage(id) {
    $.getJSON('/Canvas/getPath?id=' + id, function (data) {
        if (data != '') {
            var ext = /[^.]+$/.exec(data);
            var filename = data;
            if(ext == 'tif')
            {
                //window.open('/DocViewer/DocViewer.aspx?FilePath=' + data, 'WindowPopup', 'height=auto,width=auto,status=no,toolbar=no,resizable=yes,scrollbars=yes');
                window.open('/DocViewer/DocViewer.aspx', 'WindowPopup', 'height=auto,width=auto,status=no,toolbar=no,resizable=yes,scrollbars=yes');
            }
            else if(ext == 'xml'){                    
                $.getJSON('/Canvas/getXml?filePath=' + filename, function (xmldata) {
                    alert(xmldata);
                    window.open('data:text/xml,' + encodeURIComponent(xmldata), "WindowPopup", 'height=auto,width=auto,status=no,toolbar=no,resizable=yes,scrollbars=yes');
                });                    
            }
        }
        else {
            alert('Document does not exists. Please make sure the document is on location.');
        }
    });
};

【讨论】:

    【解决方案2】:
    $.ajax({
          url: '@Url.Action("getXml", "Canvas")'+'?filePath'+=filename,
          type: 'GET',
          success: function(data) 
          { 
          alert(data); 
          }
    });
    

    您的代码无法正常工作并给出未定义的参数,因为您正在混合使用 javascript 和 razor 方法调用。

    function showImage(id) {
        $.get('/Canvas/getPath?id=' + id, function (data) {
            if (data != '') {
                var ext = /[^.]+$/.exec(data);
                var filename = data;
                if(ext == 'tif')
                {                    
                    //window.open('/DocViewer/DocViewer.aspx?FilePath=' + data, 'WindowPopup', 'height=auto,width=auto,status=no,toolbar=no,resizable=yes,scrollbars=yes');
                    window.open('/DocViewer/DocViewer.aspx', 'WindowPopup', 'height=auto,width=auto,status=no,toolbar=no,resizable=yes,scrollbars=yes');
                }
                else if(ext == 'xml'){
                    $.ajax({
                    url: '@Url.Action("getXml", "Canvas")'+'?filePath'+=filename,
                    type: 'GET',
                    success: function(data) 
                    { 
                    alert(data); 
                    }
                    });
                    //window.open(data, "WindowPopup", 'height=auto,width=auto,status=no,toolbar=no,resizable=yes,scrollbars=yes');
                }
            }
            else {
                alert('Document does not exists. Please make sure the document is on location.');
            }
        });
    };
    

    【讨论】:

    • 我应该把这个放在哪里?在 else 块里面?
    • 是的。您必须放在那里才能使该链接正常工作
    • 我只想在文件ext 是xml 时调用它。在 else if 块中。如果我在else if 内部调用它。然后代码不起作用。
    • 是的。如果检查扩展名是 xml,则必须在 else if 块中放置。问题是代码的一部分而不是整个代码。
    • @dotnetstep 我将我的代码更新为你的。但我不工作。我的旧代码也不起作用。是否可以在$.get 内定义$.ajax
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-12
    • 2016-04-08
    • 2020-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多