How to format JSON for asp.net webmethod that takes class parameter

前端 未结 2 703
耶瑟儿~
耶瑟儿~ 2020-12-21 11:51

I have the following webmethod on my asp.net code behind page:

[WebMethod(EnableSession = true)]
public static bool SaveFailureData(SimpleFailureData data)
{         


        
2条回答
  •  礼貌的吻别
    2020-12-21 12:31

    ok try this

        public static bool SaveFailureData(string sampleFailure)
        {
            JavaScriptSerializer s = new JavaScriptSerializer();
            SimpleFailureData sdata = s.Deserialize(sampleFailure);
            return true;
        }
    
        var json = {
                    "Comments": "",
                    "Score": 66.66666666666667,
                    "Adjustment": 0,
                    "ShutdownData": [{ "Id": "401", "CausedShutdown": true, "ShutdownType": "NORMAL"}]
                }
    
                var data = JSON.stringify(json);
    
                $.ajax({
                    type: "POST",
                    url: 'Default.aspx/SaveFailureData',
                    contentType: 'application/json; charset=utf-8',
                    data: "{'sampleFailure' : '" + data + "'}",
                   // data: data,
                    dataType: 'json',
                    success: function (msg) {
                        alert(msg.d);
                    },
                    error: function (msg) {
                        alert('Error!');
                    }
                });
    
            });
    

    you will get the data in sdata object.

提交回复
热议问题