Json结果作为Internet Explorer中的文件返回?

时间:2014-07-15 11:21:57

标签: ajax asp.net-mvc json

net MVC应用程序,其中我有多个图表。在这些图表上,我已应用过滤器并通过单击每个过滤器,我执行ajax调用,该结果在Json中返回结果,然后应用于图表。

现在它在Firefox和Chrome中完美运行,但在Internet Explorer中 - Ajax调用总是不成功。我尝试直接通过我的浏览器访问web api url并且看起来是问题,结果json被返回作为要下载的文件。

这是我的ajax代码:

function getIssueResolvedGraphdata(control, departCode, departName) {
$.ajax(
    {
        type: "GET",
        url: WebApiURL + "/api/home/GetQueryIssueResolvedData?deptCode=" + departCode,
        dataType: "json",
        crossDomain: true,
        async: true,
        cache: false,
        success: function (myData) {
            var resolvedStartDate = myData.data.IssueResolvedStartDate;
            var issueData = myData.data.IssueData;
            var resolveData = myData.data.ResolvedData;

            //converting issueData into integer array...
            var issue = issueData.replace("[", "");
            var issue1 = issue.replace("]", "");
            var issue2 = issue1.split(",");
            for (var i = 0; i < issue2.length; i++) { issue2[i] = parseInt(issue2[i]); }

            //converting resolvedData into integer array
            var resolve = resolveData.replace("[", "");
            var resolve1 = resolve.replace("]", "");
            var resolve2 = resolve1.split(",");
            for (var j = 0; j < resolve2.length; j++) { resolve2[j] = parseInt(resolve2[j]); }

            //getting max value from array...
            var issueMaxVal = Math.max.apply(null, issue2);
            var resolveMaxVal = Math.max.apply(null, resolve2);

            //Eliminating leading zeros in issue array
            var removeIndex = 0;
            var myDate;
            var newDate;
            var arrayLength;
            if (issueMaxVal != 0) {
                arrayLength = issue2.length;
                for (var i = 0; i < arrayLength; i++) {
                    if (issue2[0] == 0) {
                        issue2.splice(0, 1);
                        removeIndex = i;
                    } else {
                        break;
                    }
                }

                //Getting days count of current month
                var monthStart = new Date(new Date().getFullYear(), new Date().getMonth(), 1);
                var monthEnd = new Date(new Date().getFullYear(), new Date().getMonth() + 1, 1);
                var monthLength = (monthEnd - monthStart) / (1000 * 60 * 60 * 24);
                var monthDays = 0;
                if (monthLength == 28) {
                    monthDays = removeIndex;
                }
                else if (monthLength == 30) {
                    monthDays = removeIndex + 1;
                }
                else if (monthLength == 31 || monthLength == 29) {
                    monthDays = removeIndex + 2;
                }

                //matching the resultant issue array with resolve array & setting start date
                var iDate = resolvedStartDate;
                var tDate = '';
                for (var i = 0; i < iDate.length; i++) {
                    if (iDate[i] == ',') {
                        tDate += '/';
                    }
                    else {
                        tDate += iDate[i];
                    }
                }
                if (removeIndex != 0) {
                    resolve2.splice(0, (removeIndex + 1));
                    var myDate = new Date(tDate);
                    myDate.setDate(myDate.getDate() + monthDays);
                    newDate = Date.UTC(myDate.getFullYear(), (myDate.getMonth() + 1), myDate.getDate());
                } else {
                    var myDate = new Date(tDate);
                     newDate = Date.UTC(myDate.getFullYear(), (myDate.getMonth() + 1), myDate.getDate());
                }

            } else {
               alert("Empty");
            }

            //updating chart here...
            var chart = $('#performance-cart').highcharts();
            chart.series[0].update({
                pointStart: newDate,
                data: issue2
            });
            chart.series[1].update({
                pointStart: newDate,
                data: resolve2
            });


            if (issueMaxVal > resolveMaxVal) {
                chart.yAxis[0].setExtremes(0, issueMaxVal);
            } else {
                chart.yAxis[0].setExtremes(0, resolveMaxVal);
            }
        },
        error: function (x, e) {
            alert('There seems to be some problem while fetching records!');
        } });}

来自web api控制器的代码:

 [HttpGet]
 [CrossDomainActionFilter]
public Response<GraphIssueResolvedWrapper> GetQueryIssueResolvedData(string deptCode)
{
  Response<GraphIssueResolvedWrapper> objResponse = new Response<GraphIssueResolvedWrapper>();
  GraphIssueResolvedWrapper objGraphIssueResolvedWrapper = new GraphIssueResolvedWrapper();
    try
    {
        ....code.....
        objResponse.isSuccess = true;
        objResponse.errorDetail = string.Empty;
        objResponse.data = objGraphIssueResolvedWrapper;
    }
    catch (Exception ex)
    {
        objResponse.isSuccess = false;
        objResponse.errorDetail = ex.Message.ToString();
        objResponse.data = null;
    }
    return objResponse;
}

回复课程:

public class Response<T>
{
    public bool isSuccess { get; set; }
    public string errorDetail { get; set; }
    public T data { get; set; }
}

我现在被困在这几个小时了。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

你错过了 contentType:'text / html',这对于IE7-8非常重要:

$.ajax(
    {
        type: "GET",
        url: WebApiURL + "/api/home/GetQueryIssueResolvedData?deptCode=" + departCode,
        dataType: "json",
        contentType: 'text/html'
        crossDomain: true,
        async: true,
        cache: false,
        success: function (myData) {
            var result = JSON.parse(myData);
            ///...code...
        },
        error: function (x, e) {
            alert('There seems to be some problem while fetching records!');
        }
    }
);

要使其在IE7-8中运行,您还需要确保在服务器端的响应中编写Conrent-Type Header。在return语句之前添加此行;

response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html; charset=iso-8859-1");

在代码中,您可能需要使用success

解析JSON.parse(myData);方法中的结果

答案 1 :(得分:0)

我通过使用以下代码解决了我的问题:(我想它需要CORS支持)

function isIE() {
            var ua = window.navigator.userAgent;
            var msie = ua.indexOf("MSIE");
            if (msie > 0)
                return true;
            return false;
        }

然后在我的绑定脚本的document.ready函数中:

    $(document).ready(function () {
        if (isIE())
            $.support.cors = true;
    });

注意:它仍然将Json流下载为文件,但现在每次点击时我的AJAX调用都成功。