栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用jQuery成功将JSON对象发送到ASP.NET WebMethod

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用jQuery成功将JSON对象发送到ASP.NET WebMethod

使用AJAX.NET时,我总是使输入参数只是一个普通的旧对象,然后使用javascript反序列化器将其转换为所需的任何类型。至少以这种方式,您可以调试并查看Web方法正在接收的对象类型。

使用jQuery时需要将对象转换为字符串

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server">    <title></title></head><body>    <form id="form1" runat="server">        <asp:scriptManager ID="sm" runat="server" EnablePageMethods="true"> <scripts>     <asp:scriptReference Path="~/js/jquery.js" /> </scripts>        </asp:scriptManager>        <div></div>    </form></body></html><script type="text/javascript" language="javascript">    var items = [{ compId: "1", formId: "531" },        { compId: "2", formId: "77" },        { compId: "3", formId: "99" },        { status: "2", statusId: "8" },        { name: "Value", value: "myValue"}];        //Using Ajax.Net Method        PageMethods.SubmitItems(items, function(response) { var results = response.d; }, function(msg) { alert(msg.d) }, null);        //using jQuery ajax Method        var options = { error: function(msg) { alert(msg.d); },  type: "POST", url: "WebForm1.aspx/SubmitItems",  data: {"items":items.toString()}, // array to string fixes it *  contentType: "application/json; charset=utf-8",  dataType: "json",  async: false,   success: function(response) { var results = response.d; } };         jQuery.ajax(options);</script>

和背后的代码

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Services;using System.Web.script.Serialization;using System.Web.script.Services;using System.Web.UI;using System.Web.UI.WebControls;namespace CustomEquip{    [scriptService]    public partial class WebForm1 : System.Web.UI.Page    {        protected void Page_Load(object sender, EventArgs e)        {        }        [WebMethod]        public static void SubmitItems(object items)        { //break point here List<object> lstItems = new JavascriptSerializer().ConvertToType<List<object>>(items);        }    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/485227.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号