【问题标题】:JSON data not being sent in AJAX POST requestJSON 数据未在 AJAX POST 请求中发送
【发布时间】:2017-09-19 19:10:33
【问题描述】:

我有一个 AJAX 请求,它使用 POST 请求将 JSON 对象从 MVC 视图发送到控制器:

function sendData(subscriptionJson) {
                $.ajax({
                    type: "POST",
                    url: '@Url.Action("SubscribeSecurities", "Subscription")',
                    data: '{"subscriptions": ' + subscriptionJson + '}',
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (response) {
                        console.log("success response: " + response.responseText);
                        alert("Hello: " + response.Name + " .\nCurrent Date and Time: " + response.DateTime);

                    },
                    failure: function (response) {
                        console.log("failure response: " + response.responseText);
                        alert(response.responseText);
                    },
                    error: function (response) {
                        console.log("error response: " + response.responseText);
                        alert(response.responseText);
                    }
                });
            }

控制器动作的定义如下:

[HttpPost]
        public ActionResult SubscribeSecurities(string subscriptions)
        {

JSON 格式如下:

{
    "Subscriptions": {
        "Obj1": {
            "Value1": "3454234",
            "Value2": "345643564",
            "Value3": "665445",
            "Value4": "True"
        },
        "Obj2": {
            "Value1": "3454234",
            "Value2": "345643564",
            "Value3": "665445",
            "Value4": "True"
        },
        "Obj3": {
            "Value1": "3454234",
            "Value2": "345643564",
            "Value3": "665445",
            "Value4": "True"
        }
    }
}

什么可能导致问题?

编辑

这是我在创建对象以存储 JSON POST 请求返回的值后所做的更新。

JSON

var test = {

                    "Obj1": {
                        "Value1": "3454234",
                        "Value2": "345643564",
                        "Value3": "665445",
                        "Value4": "True"
                    },
                    "Obj2": {
                        "Value1": "3454234",
                        "Value2": "345643564",
                        "Value3": "665445",
                        "Value4": "True"
                    },
                    "Obj3": {
                        "Value1": "3454234",
                        "Value2": "345643564",
                        "Value3": "665445",
                        "Value4": "True"
                    }

                }

捕获 JSON 的模型

   public class RootObject {
    // Give this a better name.  RootObject is a horrible name.

        public IEnumerable<SubscriptionObj> Subscriptions { get; set; }
}

public class SubscriptionObj
{
    public Int64 Value1 {get;set;}
    public Int64 Value2 {get;set;}
    public Int64 Value3 {get;set;}
    public Boolean Value4 {get;set;}
}

【问题讨论】:

  • data: { subscriptions: subscriptionJson },
  • @Spectarion 数据值实际上按照this posts工作
  • 报告任何错误?
  • @Komal 其实我错过了这个错误,我会用它更新帖子
  • subscriptionJson 是字符串吗?该 subscriptionJson 将被序列化为一个复杂的对象,它!= 字符串。

标签: jquery json ajax asp.net-mvc


【解决方案1】:

您的控制器需要一个string,但您正在向它发送一个自定义对象。

要么将您的 json 更改为字符串,要么将控制器期望的内容更改为与正在发送的内容相匹配的对象...例如:

public class RootObject {
    // Give this a better name.  RootObject is a horrible name.

    public Subscriptions Subscriptions {get;set;} = new Subscriptions();
}


public class Subscriptions {
    public Subscription Obj1 {get;set;} = new Subscription();
    public Subscription Obj2 {get;set;} = new Subscription();
    public Subscription Obj3 {get;set;} = new Subscription();
}


public class Subscription {
    public Int64 Value1 {get;set;}=0;
    public Int64 Value2 {get;set;}=0;
    public Int64 Value3 {get;set;}=0;
    public Boolean Value4 {get;set;}=false;
}

您的 MVC 控制器会自动将传入的 json 字符串反序列化为真实对象。如果反序列化的对象不是字符串,它不会将传入的 json 传递给 Action 的字符串参数。

【讨论】:

  • 好的,我需要创建对象来表示 JSON 对象层次结构还是 C# 中有可以存储 JSON 的数据类型?
  • 我刚刚创建了这些对象(具有不同的名称),但它似乎仍然无法正常工作。我向 Subscription 对象添加了一些具有默认值的属性。这会导致问题吗?
  • 我看不到你做了什么,所以我不能告诉你哪里出了问题。您是否更改了 Action 的方法签名以接受您的新类型?
  • 没有 Json 数据类型。如果有的话会不会很酷。
  • 我删除了语句中的 = new Subscriptions() 部分,因为它们会导致智能感知错误
猜你喜欢
  • 2020-01-11
  • 2011-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多