Ajax调用给出内部服务器错误 [英] Ajax call Gives internal server error

查看:162
本文介绍了Ajax调用给出内部服务器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要显示一个进度条图像当我创建一个Excel文件。我使用Ajax调用打电话给我控制器的操作方法。它给我内部服务器错误。 这里是我的控制器/操作方法和Ajax调用.....

 公众的ActionResult GenerateExcelReport(GenerateReportViewModel oGenerateReportViewModel){
    //创建新的Excel应用程序
    excelApp =新Microsoft.Office.Interop.Excel.Application();
    excelApp.Visible = FALSE;
    excelApp.DisplayAlerts = FALSE;
    // Excel中生成code到这里....
    返回JSON(oGenerateReportViewModel);
}
 

AJAX调用....

  $(文件)。就绪(函数(){
    $(#btnSubmitGenerateRpt)。点击(函数(){
        $(#divLoading)显示()。
        $阿贾克斯({
            网址:'/ GenerateReport / GenerateExcelReport',
            键入:POST,
            数据: {},
            数据类型:JSON,
            的contentType:应用/ JSON的;字符集= UTF-8,
            错误:函数(XHR,ajaxOptions,thrownError){
                警报('错误:'+ xhr.statusText +抛出错误:+ thrownError +选项:+ ajaxOptions);
            },
            成功:函数(结果){
                警报(功能调用!!!);
                $(#divLoading)隐藏()。
            },
            异步:真正的,
            过程数据:假的
        });
    });
});
 

解决方案

当你需要传递JSON到你的行动,你必须通过一个字符串重的JSON,通常与JSON.stringify的presentation:

  $。阿贾克斯({
    ...
    数据:JSON.stringify({}),
    ...
});
 

或课程,因为你传递什么,你可以做

 数据:{},
 

如果您使用的是默认的的contentType 代替(应用程序/ x-WWW的形式urlen codeD;字符集= UTF-8),那么你就可以通过在一个javascript对象,并假设这是一个简单的对象,它会转化,为URL-CN codeD键值为您的文章内容。对于工作,你也必须删除您过程数据:假

I want to show a progress bar image when I create an Excel file. I am using an ajax call to call my controller action method. It gives me Internal Server Error. Here is my controller/Action method and Ajax call .....

public ActionResult GenerateExcelReport(GenerateReportViewModel oGenerateReportViewModel) {
    // Create new excel application
    excelApp = new Microsoft.Office.Interop.Excel.Application();
    excelApp.Visible = false;
    excelApp.DisplayAlerts = false;
    // Excel Generation Code goes here....
    return Json(oGenerateReportViewModel);
}

AJAX Call....

$(document).ready(function () {
    $("#btnSubmitGenerateRpt").click(function () {
        $("#divLoading").show();
        $.ajax({
            url: '/GenerateReport/GenerateExcelReport',
            type: 'POST',
            data: {},
            dataType: 'json',
            contentType: 'application/json; charset=utf-8',
            error: function (xhr, ajaxOptions, thrownError) {
                alert('Error: ' + xhr.statusText + " Thrown Error: " + thrownError + " Options: " + ajaxOptions);
            },
            success: function (result) {
                alert("Function Called!!!");
                $("#divLoading").hide();
            },
            async: true,
            processData: false
        });
    });
});

解决方案

When you need to pass JSON to your action, you have to pass a string representation of the JSON, typically with JSON.stringify:

$.ajax({
    ...
    data: JSON.stringify({}),
    ...
});

Or of course, since you're passing nothing, you can just do

    data: "{}",

If you're using the default contentType instead (application/x-www-form-urlencoded; charset=UTF-8), then you can pass in a javascript object, and assuming it's a simple object, it will translate that to URL-encoded key-values for your POST contents. For that to work, you'd have to also remove your processData:false line.

这篇关于Ajax调用给出内部服务器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆