JSON数据未在AJAX POST请求中发送 [英] JSON data not being sent in AJAX POST request

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

问题描述

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

I have an AJAX request that is sending a JSON object from a MVC View to a Controller using a POST request:

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);
                    }
                });
            }

控制器动作具有以下定义:

The controller action has the following definition:

[HttpPost]
        public ActionResult SubscribeSecurities(string subscriptions)
        {

JSON具有以下格式:

The JSON has the following format:

{
    "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请求返回的值之后进行的更新.

Here are the updates that I made after creating objects to store the values returned by the JSON POST request.

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;}
}

推荐答案

您的控制器期望使用string,但是您正在向其发送自定义对象.

Your controller is expecting a string, but you are sending it a custom object.

将json更改为字符串,或者将控制器期望的对象更改为与发送对象匹配的对象...例如:

Either change your json to a string, or change what your controller is expecting to an object that matches what is being sent... for example:

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的string参数.

Your MVC controller will automatically deserialize the incoming json string into a real object. If the deserialized object is NOT a string, it will not pass along the incoming json to your Action's string parameter.

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

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