Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从javascript调用控制器方法_Javascript_C#_Jquery_Xml_Asp.net Mvc 4 - Fatal编程技术网

从javascript调用控制器方法

从javascript调用控制器方法,javascript,c#,jquery,xml,asp.net-mvc-4,Javascript,C#,Jquery,Xml,Asp.net Mvc 4,我想从jquerycall调用控制器方法。这是我的code <script> function showImage(id) { $.get('/Canvas/getPath?id=' + id, function (data) { if (data != '') { var ext = /[^.]+$/.exec(data); var filename = data; if(ext ==

我想从
jquery
call调用控制器方法。这是我的
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>
这里是我想要的如果
ext
如果是xml,那么我想调用
控制器的
方法。在上面的代码中,它给出了关于
new{filePath=filename}
filename不存在的错误。基本上,我试图在浏览器中打开xml文件。在数据中,我接收xml文件的文件路径。如何调用控制器方法或在浏览器中打开xml文件

$.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.');
        }
    });
};

嗨,伙计们,我的问题解决了。这里是我的完整
脚本

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

函数showImage(id){
$.getJSON('/Canvas/getPath?id='+id,函数(数据){
如果(数据!=''){
var ext=/[^.]+$/.exec(数据);
var filename=数据;
如果(ext==“tif”)
{
//window.open('/DocViewer/DocViewer.aspx?文件路径='+数据,'WindowPopup','height=auto,width=auto,status=no,toolbar=no,resizeable=yes,scrollbars=yes');
window.open('/DocViewer/DocViewer.aspx',WindowPopup',height=auto,width=auto,status=no,toolbar=no,resizeable=yes,scrollbars=yes');
}
else if(ext=='xml'){
$.getJSON('/Canvas/getXml?filePath='+文件名,函数(xmldata){
警报(xmldata);
open('data:text/xml,+encodeURIComponent(xmldata),'WindowPopup','height=auto,width=auto,status=no,toolbar=no,resizeable=yes,scrollbars=yes');
});                    
}
}
否则{
警报('文档不存在。请确保文档位于位置');
}
});
};

您正在从本地系统打开一个文件。在浏览器安全实践中不允许使用,除非它来自上传的文件。如果我们能做到这一点,那么我们所有的开发人员都可以在本地访问用户文件。这就是为什么我试图调用返回xml字符串的控制器方法。因此,我可以在browserA中展示它,更好的方法是解析XML并将内容作为XML或JSON返回给客户端,而不是文件本身。但我不能在其他内部调用控制器方法block@agentpx如果
块,我如何调用
中的控制器方法?我应该把它放在哪里?在其他街区吗?是的。只有当文件
ext
xml
时,我才想调用它。在else if块中。如果我在
内调用它,否则如果
。那么代码就不起作用了。若检查扩展名为xml的块,则必须放置else。问题是代码的一部分而不是整个代码。@dotnetstep我将我的代码更新为您的代码。但我不工作。我的旧代码也不起作用。是否可以在
$.get
<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.');
        }
    });
};