MVC发送列表通过AJAX列表、MVC、AJAX

2023-09-11 22:28:15 作者:以可爱出名

好吧,我见过吨问题发布关于这个问题,但没有一个真正为我工作的答案,这里是我的AJAX:

  $。阿贾克斯({
        网址:/ FilterSessions / GetFilterSession
        键入:GET,
        数据类型:JSON,
        数据:jsonFilters,
        传统:真正的,
        成功:函数(响应){
            //哈哈,从来没有进入这里。不是真的。
        }
    });
 

变种jsonFilters包含具有以下数据的数组:

  [0] = {路径:测试,产品名称:更多测试,值:测试值},
[1] = {路径:测试,产品名称:更多测试,值:测试值}
 

这是我的控制器:

 公众的ActionResult GetFilterSession(名单< FilterSessionModel> jsonFilters)
{
    //做的事情

    返回JSON(假,JsonRequestBehavior.AllowGet);
}
 
ajax url 请求发送不到spring mvc controller

jsonFilters始终保持空...我也尝试添加的contentType:应用/ JSON的;字符集= UTF-8的AJAX调用...但是,这真的没有做什么

最后,类 FilterSessionModel 的结构如下:

 公共类FilterSessionModel
    {
        公共字符串路径{获得;组; }
        公共字符串名称{;组; }
        公共字符串值{获得;组; }
    }
 

任何想法,我可能会丢失或可能发生的事?

东西我试过到目前为止:

设置传统:真正的设置的contentType,使用JSON.stringify并试图接受一个字符串中的MVC控制器(NO-GO)

更新:由于下面我回答意识到,缺少的是送过来的参数ID如下数据:

 数据:{param1ID:+ param1Val +}
 

解决方案

我会尝试切换出类型上你的行动。

 名单,其中,FilterSessionModel>
 

pretty的确定上面是行不通的,我会尝试类似的对象。

也可能是字符串,然后我会用牛顿JSON DLL推到你的类。名单

这个问题归结到你的行动暂时无法断定类型,假设你正在检查你的数据之前,阿贾克斯获得被调用。

**更新,由于更多的信息。添加在错误部分和查看一些从控制器返回的增值经销商,也火了小提琴手,看你做了什么为HTTP号码。

  $。阿贾克斯({
    键入:POST,
    网址:Servicename.asmx / DoSomeCalculation
  数据:{param1ID:+ param1Val +},
    的contentType:应用/ JSON的;字符集= UTF-8,
    数据类型:JSON,
    成功:函数(MSG){
        UseReturnedData(msg.d);
    },
    错误:函数(X,T,M,B){
        //看看上面的增值经销商,看看有什么是他们。
    }
});
 

Okay, I've seen tons of questions posted regarding this question, but none of the answers has actually worked for me, here's my AJAX:

$.ajax({
        url: "/FilterSessions/GetFilterSession",
        type: "GET",
        dataType: "json",
        data: jsonFilters,
        traditional: true,
        success: function (response) {
            //Haha, it's never entering here. not really.
        }
    });

var "jsonFilters" contains an array with the following data:

[0] = { Path: "Test", Name: "More testing", Value: "Test Value" },
[1] = { Path: "Test", Name: "More testing", Value: "Test Value" } 

And this is my controller:

public ActionResult GetFilterSession(List<FilterSessionModel> jsonFilters)
{
    //Do things

    return Json(false, JsonRequestBehavior.AllowGet);
}

jsonFilters always remains null... I have also tried adding contentType: "application/json; charset=utf-8" to the AJAX call... but that didn't really do anything

Finally, the class FilterSessionModel is structured as follows:

 public class FilterSessionModel
    {
        public string Path { get; set; }
        public string Name { get; set; }
        public string Value { get; set; }
    }

Any ideas as to what I might be missing or what might be happening?

Things I've tried so far:

Setting "traditional: true", setting "contentType", using JSON.stringify and attempting to accept a string in the MVC Controller (no-go)

UPDATE: Thanks to the answer below I realized that what was missing was to send over the data with the param Id like so:

 data: "{param1ID:"+ param1Val+"}"

解决方案

I would try switching out the type on your action.

List<FilterSessionModel>

Pretty sure the above is not going to work, I would try something like Object.

Or possibly a string that I would then use newton json dll to push into your List of Class.

The problem boils down to your action being unable to figure out the type, assuming you are checking your data prior to the ajax get being called.

**Update due to more info. Add in the error portion and view those vars on return from your controller, also fire up fiddler and watch what your are getting for http numbers.

$.ajax({
    type: "POST",
    url: "Servicename.asmx/DoSomeCalculation", 
  data: "{param1ID:"+ param1Val+"}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
        UseReturnedData(msg.d);
    },
    error: function(x, t, m, b) {
        //Look at the vars above to see what is in them.
    }
});