即使存在JsonResult方法,也找不到页面

时间:2016-03-02 07:15:40

标签: jquery ajax asp.net-mvc asp.net-mvc-4 http-status-code-404

我正在使用MVC应用程序,在调用JsonResult AJAX时遇到404错误

我的要求:

http://localhost:60890/ED/Document/GetGroupDocumentMergedPath?groupDocuments=%5B%7B%22GroupDocumentId%22%3A1314%2C%22TemplateId%22%3A212%2C%22GroupDocumentType%22%3A0%7D%2C%7B%22GroupDocumentId%22%3A149%2C%22TemplateId%22%3A208%2C%22GroupDocumentType%22%3A0%7D%2C%7B%22GroupDocumentId%22%3A570%2C%22TemplateId%22%3A210%2C%22GroupDocumentType%22%3A0%7D%2C%7B%22GroupDocumentId%22%3A198%2C%22TemplateId%22%3A209%2C%22GroupDocumentType%22%3A0%7D%2C%7B%22GroupDocumentId%22%3A90%2C%22TemplateId%22%3A201%2C%22GroupDocumentType%22%3A0%7D%2C%7B%22GroupDocumentId%22%3A119%2C%22TemplateId%22%3A206%2C%22GroupDocumentType%22%3A0%7D%2C%7B%22GroupDocumentId%22%3A2431%2C%22TemplateId%22%3A217%2C%22GroupDocumentType%22%3A0%7D%2C%7B%22GroupDocumentId%22%3A1004%2C%22TemplateId%22%3A211%2C%22GroupDocumentType%22%3A0%7D%2C%7B%22GroupDocumentId%22%3A1686%2C%22TemplateId%22%3A213%2C%22GroupDocumentType%22%3A0%7D%2C%7B%22GroupDocumentId%22%3A2076%2C%22TemplateId%22%3A215%2C%22GroupDocumentType%22%3A0%7D%2C%7B%22GroupDocumentId%22%3A2469%2C%22TemplateId%22%3A302%2C%22GroupDocumentType%22%3A0%7D%2C%7B%22GroupDocumentId%22%3A5140%2C%22TemplateId%22%3A220%2C%22GroupDocumentType%22%3A0%7D%2C%7B%22GroupDocumentId%22%3A5020%2C%22TemplateId%22%3A221%2C%22GroupDocumentType%22%3A0%7D%2C%7B%22GroupDocumentId%22%3A5105%2C%22TemplateId%22%3A222%2C%22GroupDocumentType%22%3A0%7D%2C%7B%22GroupDocumentId%22%3A5066%2C%22TemplateId%22%3A223%2C%22GroupDocumentType%22%3A0%7D%2C%7B%22GroupDocumentId%22%3A4039%2C%22TemplateId%22%3A225%2C%22GroupDocumentType%22%3A0%7D%2C%7B%22GroupDocumentId%22%3A4159%2C%22TemplateId%22%3A235%2C%22GroupDocumentType%22%3A0%7D%2C%7B%22GroupDocumentId%22%3A4283%2C%22TemplateId%22%3A245%2C%22GroupDocumentType%22%3A0%7D%2C%7B%22GroupDocumentId%22%3A4343%2C%22TemplateId%22%3A249%2C%22GroupDocumentType%22%3A0%7D%2C%7B%22GroupDocumentId%22%3A4399%2C%22TemplateId%22%3A250%2C%22GroupDocumentType%22%3A0%7D%2C%7B%22GroupDocumentId%22%3A4463%2C%22TemplateId%22%3A252%2C%22GroupDocumentType%22%3A0%7D%2C%7B%22GroupDocumentId%22%3A4856%2C%22TemplateId%22%3A256%2C%22GroupDocumentType%22%3A0%7D%2C%7B%22GroupDocumentId%22%3A4906%2C%22TemplateId%22%3A257%2C%22GroupDocumentType%22%3A0%7D%2C%7B%22GroupDocumentId%22%3A4956%2C%22TemplateId%22%3A258%2C%22GroupDocumentType%22%3A0%7D%2C%7B%22GroupDocumentId%22%3A2499%2C%22TemplateId%22%3A215%2C%22GroupDocumentType%22%3A0%7D%5D&actionType=715&_=1456902108180

控制器:

 [HttpGet]
 public JsonResult GetGroupDocumentMergedPath(string groupDocuments, int actionType){}

AJAX致电:

$.ajax({
    url: '/ED/Document/GetGroupDocumentMergedPath',
    type: "GET",
    data: { groupDocuments: JSON.stringify(groupDocuments), actionType: 715 },
    beforeSend: function () {
    },
    success: function (data) {
        //Business logic
    },
    complete: function () {
        checkedIds = {};
        ToggleDisabled("print", false);
    }
});

情境:

在AJAX调用中,我发送一个平均有20个具有三个属性的对象的列表。

我的查询字符串长度超过导致错误的允许值或者我错在哪里?

1 个答案:

答案 0 :(得分:0)

不要在单个字符串中传递多个细节。你必须创建一个ViewModel并传递给Data中的模型的serializeObject。所以通过AJAX将数据传递给Method是安全而好的方法。

如果你想从表单控件传递数据,那么序列化表单并将其传递给AJAX。

请查看以下示例。 Ajax调用

$.fn.serializeObject = function () {
            var o = {};
            var a = this.serializeArray();
            $.each(a, function () {
                if (o[this.name] !== undefined) {
                    if (!o[this.name].push) {
                        o[this.name] = [o[this.name]];
                    }
                    o[this.name].push(this.value || '');
                } else {
                    o[this.name] = this.value || '';
                }
            });
            return o;
        };
    form = $('#groupDocuments').serializeObject(); // groupDocuments is form Id
    var data = JSON.stringify({ model: form });    
    $.ajax({
        url: '/ED/Document/GetGroupDocumentMergedPath',
        type: "GET",
        data: data,
        beforeSend: function () {
        },
        success: function (data) {
            //Business logic
        },
        complete: function () {
            checkedIds = {};
            ToggleDisabled("print", false);
        }
    });

JasonResult方法

  [HttpPost]
    public JsonResult GetGroupDocumentMergedPath(GroupDocumentViewModel model)
    {
    // Your Code here       
    }

查看模型

public class GroupDocumentViewModel
{   
//All your property which you passing from view to controller via AJAX  
}