js向后台传递对象

时间:2022-08-26 10:05:53

js:

var obj = { 'a': "1", "b": 5 };
        $.ajax({
            url: "/.../...",
            type: "POST",
            async: false,
            data: JSON.stringify(obj),
            success: function (result) {
                alert(result);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert(XMLHttpRequest.status);
                alert(XMLHttpRequest.readyState);
                alert(textStatus);
            }
        });

后台:

[HttpPost]
        public ActionResult Test()
        {
            var sr = new StreamReader(Request.InputStream);
            string josnStr = sr.ReadToEnd();
            JavaScriptSerializer js = new JavaScriptSerializer();
            object requestData = js.Deserialize<object>(josnStr);
            return Json(true);
        }