我想从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,那么我想要调用method
的{{1}}。在上面的代码中,它给出了controller
文件名不存在的错误。基本上我试图在浏览器中打开 xml文件。在数据中,我收到了xml文件的 filePath 。如何在浏览器中调用控制器方法或打开xml文件?
答案 0 :(得分:0)
$.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.');
}
});
};
答案 1 :(得分:0)
大家好我的问题解决了。在这里我的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.');
}
});
};