【问题标题】:Ajax request produces internal server errorAjax 请求产生内部服务器错误
【发布时间】:2014-03-09 17:47:47
【问题描述】:

我正在使用 ace 编辑器在浏览器上启用代码编辑。现在有一个保存功能,当用户单击它时,我使用 ajax 请求将代码从编辑器发送回服务器。但我在检查浏览器控制台时遇到内部服务错误。

Ajax 客户端代码

$('#save-button').click(function () {

            $.post('/CodeEditor/SaveFile',
            {
                'file': $('#current-file').val(),
                'content': editor.getValue()
            }, function () {

            },
            'JSON');
});

Ajax 服务器代码

    [HttpPost]
    public ActionResult SaveFile(string file, string content)
    {
        return null;
    }

以下是我在 content 参数中通过 Ajax 提交的文件的内容

@model IList<ShopifyHome.Models.TemplatePartial>

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>ModifyTemplate</title>
    <link rel="stylesheet" type="text/css" href="../../Content/aciTree    /css/aciTree.css" media="all" />
    <link rel="stylesheet" type="text/css" href="../../Content/aciTree/css/demo.css" media="all" />
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
    @*<script type="text/javascript" src="../../Content/aciTree/js/jquery.min.js"></script>*@
    <script type="text/javascript" src="../../Content/aciTree/js/jquery.aciPlugin.min.js"></script>
    <script type="text/javascript" src="../../Content/aciTree/js/jquery.aciTree.core.js"></script>
    <script type="text/javascript" src="../../Content/aciTree/js/jquery.aciTree.selectable.js"></script>
    <script type="text/javascript" src="../../Content/aciTree/js/jquery.aciTree.checkbox.js"></script>
    <script type="text/javascript" src="../../Content/aciTree/js/jquery.aciTree.radio.js"></script>
    <style type="text/css">
    #frame-div
    {

        }

    #style-div
    {
        width:250px;
        float:right;
        height:50%;
        margin: auto auto; ...

当文件的内容很简单时,请求确实通过并在服务器端接收

This is text

【问题讨论】:

  • 您收到什么具体错误?
  • 当我在编辑器中编写复杂的内容(如您在上面看到的代码)时,我在客户端收到内部服务器错误。但是当在编辑器中编写一些简单的字符串(例如“Hello World”)时,一切正常。其实我知道JSON(由客户端提交)的反序列化(在服务器上完成)有问题
  • 尝试使用 ajax 调用更改您的帖子。在这里查看我的答案stackoverflow.com/questions/22228758/…
  • 以下代码$.ajax({type: 'POST',url: '/CodeEditor/SaveFile', data: { 'file': $('#current-file').val(), 'content': editor.getValue() }, dataType: 'json', cache: false, success: function (data) { }, async: true, error: function (erro) { console.log(erro); } });仍然存在同样的问题

标签: jquery ajax json asp.net-mvc-4 serialization


【解决方案1】:

终于搞定了。必须对 html 进行编码,然后在服务器端对其进行解码。

客户端代码

$.post('/CodeEditor/SaveFile',
{
    'file': $('#current-file').val(),
    'content': encodeURIComponent(editor.getValue())
},
function () {
},
"JSON");

服务器代码

[HttpPost]
public ActionResult SaveFile(string file, string content)
{
    content = HttpUtility.UrlDecode(content);
}

【讨论】:

    猜你喜欢
    • 2019-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-07
    • 1970-01-01
    • 2019-02-19
    相关资源
    最近更新 更多