I have an action method which returns an excel file in return. I am calling that action method using ajax. I am getting Requested JSON parse failed
.
$.ajax({
url: importUrl,
data: {
X: "12",
Y: "12",
Z: "12"
},
success: function (data) {
alert("S: "+data);
},
error: function (jqXHR, exception) {
var msg = '';
if (jqXHR.status === 0) {
msg = 'Not connect.\n Verify Network.';
} else if (jqXHR.status == 404) {
msg = 'Requested page not found. [404]';
} else if (jqXHR.status == 500) {
msg = 'Internal Server Error [500].';
} else if (exception === 'parsererror') {
msg = 'Requested JSON parse failed.';
} else if (exception === 'timeout') {
msg = 'Time out error.';
} else if (exception === 'abort') {
msg = 'Ajax request aborted.';
} else {
msg = 'Uncaught Error.\n' + jqXHR.responseText;
}
console.log(msg);
}
});
public ActionResult ExportReportToExcel(string X, string Y, string Z)
{
if (HttpContext.Request.UrlReferrer == null)
TempData["PDFPrevUrl"] = Url.RouteUrl("PageNotFound");
else if (TempData["PDFPrevUrl"] == null)
TempData["PDFPrevUrl"] = HttpContext.Request.UrlReferrer.PathAndQuery;
var customer = _authenticationService.CurrentCustomer;
if (customer == null)
return new LmsHttpUnauthorizedResult();
string filename = "Report";
try
{
XLWorkbook wb = new XLWorkbook(Server.MapPath(@"~/Content/CumulativePerformanceReportTemplate.xlsx"));
XElement userprogress = XElement.Load(Server.MapPath(@"~/Content/Export.xml")).Element("cumulativeperformancereport");
int datarow = int.Parse(userprogress.Element("T").Attribute("row").Value.Trim());
int datacol = int.Parse(userprogress.Element("T").Attribute("col").Value.Trim());
IXLWorksheet WS = wb.Worksheet(1);
WS.Cell(datarow, datacol).Value = customer.Name;
datarow = int.Parse(userprogress.Element("X").Attribute("row").Value.Trim());
datacol = int.Parse(userprogress.Element("X").Attribute("col").Value.Trim());
WS.Cell(datarow, datacol).Value = X;
datarow = int.Parse(userprogress.Element("Y").Attribute("row").Value.Trim());
datacol = int.Parse(userprogress.Element("Y").Attribute("col").Value.Trim());
WS.Cell(datarow, datacol).Value = Y;
datarow = int.Parse(userprogress.Element("Z").Attribute("row").Value.Trim());
datacol = int.Parse(userprogress.Element("Z").Attribute("col").Value.Trim());
WS.Cell(datarow, datacol).Value = Z;
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment;filename=" + filename + "_Summary.xlsx");
using (MemoryStream MyMemoryStream = new MemoryStream())
{
wb.SaveAs(MyMemoryStream);
MyMemoryStream.WriteTo(Response.OutputStream);
Response.Flush();
Response.End();
}
return null;
}
catch (Exception ex)
{
return Redirect(TempData["PDFPrevUrl"].ToString());
}
}
Why am I getting this error?