jQuery AJAX不发送数据 [英] jQuery AJAX not sending data

查看:112
本文介绍了jQuery AJAX不发送数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的jQuery/JS代码:

This is my jQuery / JS code:

var selectedMonths = [];
var selectedLocations = [];
var selectedEvents = [];

$("#filter").click(function ()
{
    $('#months li').each(function ()
    {
        if ($(this).find('input[type=checkbox]').is(':checked'))
        {
            selectedMonths.push($(this).find('a').attr('data-value'));
        }
    });

    $('#locations li').each(function () {
        if ($(this).find('input[type=checkbox]').is(':checked')) {
            selectedLocations.push($(this).find('a').attr('data-value'));
        }
    });

    $('#events li').each(function () {
        if ($(this).find('input[type=checkbox]').is(':checked')) {
            selectedEvents.push($(this).find('a').attr('data-value'));
        }
    });

    var months = { months: selectedMonths };

    $.ajax({
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        type: 'POST',
        url: '/Shop/FilterRinglet',
        data: months,
        traditional: true,
        success: function (result) {
            alert("success");
        },
        failure: function (response) {
            alert(response);
        }
    });
});

这是我的简单控制器:

[HttpPost]
public ActionResult FilterRinglet(List<string> months)
{
    return Json(new { x = "sdg", received = "ok" });
}

但是Chrome DevTools仅显示500-内部服务器错误.

But Chrome DevTools only show an 500 - Internal Server Error.

我想向该控制器发送三个字符串数组(例如,您仅看到发送一个字符串数组的尝试).

I want to send three string arrays to that controller (in example you see only the attempts of sending one).

我尝试了这个(

I tried this ( How can I post an array of string to ASP.NET MVC Controller without a form? ) and may other answers. I can´t get this to work, would you please tell me where my mistake is hidden?

修改: 我还尝试了以下代码行

I also tried the following line of code

var months = JSON.stringify({ months: selectedMonths });

selectedMonths看起来像这样

selectedMonths looks like this

那不会导致500,但是在控制器中看起来像这样:

That does not lead to a 500, but in controller it looks like this:

Edit2: 我也尝试过:

        $.post('/Shop/FilterRinglet', { 'months[]': selectedMonths }, function (response) {
            alert(response);
        });

,但只有""以外的任何内容都无法到达控制器.

but nothing except "" reaches the controller.

Edit3:

我改变了

[HttpPost]
public ActionResult FilterRinglet(List<string> months)

[HttpPost]
public ActionResult FilterRinglet(string[] months)

但仍然没有成功.

我还创建了一个课程

public class RingletData
{
    public List<string> months { get; set; }
}

,并相应地更改了方法.几个月仍然是"".

and changed the method accordingly. months is still "".

推荐答案

因此,经过数小时的研究并结合了不同的方法,我提出了以下解决方案:

So, after hours of research and combining different approaches, I came up with the following solution:

// old line, needs to be changed: var months = { months: selectedMonths };
// new line, already contains the other arrays:
var data = JSON.stringify({ months: selectedMonths, locations: selectedLocations, events: selectedEvents });

$.ajax({
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    type: 'POST',
    url: '/Shop/FilterRinglet',
    data: data,
    traditional: true,
    success: function (result) {
        alert("success");
    },
    failure: function (response) {
        alert(response);
    }
});

控制器:

    [HttpPost]
    public ActionResult FilterRinglet(RingletData data)
    {
        ...
    }

如果您使用JSON.stringify(...),重要的是不要忘记contentType: "application/json".这是我的错误,因为在不同的试验和错误中,我忘记了使用它-但是我不知道在使用JSON.stringify(...)

Important is, if you use JSON.stringify(...), to not forget contentType: "application/json". This was my mistake, because in my different trial and errors I forgot to use this - but I didn´t know it was necessary when working with JSON.stringify(...)

另外,您还应该创建一个对象,例如RingletData:

Also you should create an object, like RingletData in my case:

public class RingletData
{
    public string[] months { get; set; }
    public string[] locations { get; set; }
    public string[] events { get; set; }
}

然后可以使用data.months访问您发送的数据.

Your sent data is then accessible by using data.months.

这篇关于jQuery AJAX不发送数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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