So, I am trying to return an xml
file from my controller:
所以,我试图从我的控制器返回一个xml文件:
[HttpGet]
public ActionResult GetXml(int id)
{
var xmlBytes = _context.GetXml(id);
if (xmlBytes == null) return View("NotFound");
return File(new MemoryStream(xmlBytes), "application/xml", "file.xml");
}
This Action
is perfectly called from this ajax
call:
从这个ajax调用中完美地调用了这个Action:
$.ajax({
type: 'get',
url: url,
success: function(data) {
window.location = url;
},
error: function(error) { //error.responseText contains the actual xml string!
alert("failed to download xml");
}
});
However, instead of calling success
, it goes directly to the error
property. One interesting thing is that the responseText
property from the error
object contains the xml
that I am looking for.
但是,它不是调用成功,而是直接转到error属性。一个有趣的事情是来自错误对象的responseText属性包含我正在寻找的xml。
Could someone explain me what am I doing wrong and point me in the right direction to properly download the xml stream?
有人可以解释一下我做错了什么,并指出我正确的方向来正确下载xml流?
Thanks
1 个解决方案
#1
0
This is classic issue where the status is 200 and the responseText has the output, but it is an error. Seems strange, but there is a reason for it.
这是典型问题,其状态为200且responseText具有输出,但这是一个错误。看起来很奇怪,但有一个原因。
If you were to look at the responseXML you are not going to see the xml document since it can not be parsed by the browser for some reason. Now jQuery auto detects the content type and sees it is XML, it sees the responseXML as null and throws it into the error state. If you log the arguments you will see the error message.
如果您要查看responseXML,则不会看到xml文档,因为浏览器由于某种原因无法对其进行解析。现在jQuery自动检测内容类型并看到它是XML,它将responseXML视为null并将其抛入错误状态。如果您记录参数,您将看到错误消息。
Now to debug this you can either copy the responseText from the console or inspect the response from the network panel. Putting it into your favorite XML validator and see if it picks up the errors. Common issues is the server sticking in hidden characters into the response which chokes the parser or the server returning junk before the xml document.
现在要调试它,您可以从控制台复制responseText或检查来自网络面板的响应。将它放入您最喜欢的XML验证器中,看看它是否会发现错误。常见问题是服务器在隐藏字符中插入响应,这会阻塞解析器或服务器在xml文档之前返回垃圾。
#1
0
This is classic issue where the status is 200 and the responseText has the output, but it is an error. Seems strange, but there is a reason for it.
这是典型问题,其状态为200且responseText具有输出,但这是一个错误。看起来很奇怪,但有一个原因。
If you were to look at the responseXML you are not going to see the xml document since it can not be parsed by the browser for some reason. Now jQuery auto detects the content type and sees it is XML, it sees the responseXML as null and throws it into the error state. If you log the arguments you will see the error message.
如果您要查看responseXML,则不会看到xml文档,因为浏览器由于某种原因无法对其进行解析。现在jQuery自动检测内容类型并看到它是XML,它将responseXML视为null并将其抛入错误状态。如果您记录参数,您将看到错误消息。
Now to debug this you can either copy the responseText from the console or inspect the response from the network panel. Putting it into your favorite XML validator and see if it picks up the errors. Common issues is the server sticking in hidden characters into the response which chokes the parser or the server returning junk before the xml document.
现在要调试它,您可以从控制台复制responseText或检查来自网络面板的响应。将它放入您最喜欢的XML验证器中,看看它是否会发现错误。常见问题是服务器在隐藏字符中插入响应,这会阻塞解析器或服务器在xml文档之前返回垃圾。