【问题标题】:ajax data json using web method returns undefined使用web方法的ajax数据json返回未定义
【发布时间】:2015-10-20 06:36:44
【问题描述】:

我有一个使用 html 控件的 asp.net 网页。我想通过使用 web 方法发送输入值代码隐藏到数据库中,并且我希望页面不刷新。所以我使用了 Ajax。 在 Ajax 中,succeed 函数失败并返回 undefined。

default.aspx:

  <form id="form1" runat="server">
    <input id="hName" name="hName" type="text" />
    <input id="hSurname" name="hSurname" type="text" />
    <input id="hPhone" name="hPhone" type="text" />
    <input id="rNote" name="rNote" type="text" />
    <select id="rTypeSelect">
                  <option value="1">A</option>
                  <option value="2">B</option>
                  <option value="3">C</option>
    </select>
    <input type="button" value="Save" id="btnSaveAppointment" name="btnSaveAppointment"/>
</form>

jQuery:

 $("#btnSaveAppointment").live('click', function (event) {

        name = $('#hName').val();
        surname = $('#hSurname').val();
        phone = $('#hPhone').val();
        note = $('#rNote').val();
        type = $("#rTypeSelect option:selected").text();
        rdate = '01.09.2015';
        rhour = '09.30';
        var appointment= {};
        appointment= {
            _name: name, 
            _surname: surname,
            _phone: phone,
            _note: note,
            _type: type,
            _rdate: rdate,
            _rhour: rhour
        }

        $.ajax({
            type: 'POST',
            url: '../saveAppointment.aspx/save',
            data: { appointment: JSON.stringify(appointment) },

            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            async: true,

            success: function (result) {
                alert(result);
            },
            error: function () {
                alert('Error!' + result.d);
            }
        });
    });

saveAppointment.aspx 网络方法:

public partial class saveAppointment : System.Web.UI.Page
    {
        [System.Web.Services.WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static string save(string jsondata)
        {
            var output = JsonConvert.DeserializeObject<Appointment>(jsondata);
            Appointment r = (Appointment)output;
            r.Name= output.rName.ToString();
            //sql code here     
            return r.Name;
        } 

这是类:

public class Appointment
    {
        public int ID { get; set; }
        public string rName{ get; set; }
        public string rSurname { get; set; }
        public string rPhone{ get; set; }
        public string rNote{ get; set; }
        public string rType{ get; set; }
        public DateTime rDate{ get; set; }
        public DateTime rHour{ get; set; }
    }

当我删除 WebMethod 的参数(字符串 jsondata)时,Ajax 成功函数不会失败。但是当我输入 json 参数时,它失败了。请告诉我哪里做错了,谢谢!

【问题讨论】:

    标签: jquery asp.net ajax json webmethod


    【解决方案1】:

    看起来你对你在 C#-land 中使用什么类型的对象感到困惑。 'output' 是 Appointment 类型,因此它的成员将被称为 _name 而不是 rName。如果您检查 r 的值(从 Appointment r = (Appointment)output; 行),我认为一切都会变得清晰。

    根据您的评论 - 系统无法知道 _name 映射到 rName。您可以更改他们的名字以匹配,或者您可以执行以下操作:

    public class Appointment
    {
        ...
        [JsonProperty(PropertyName = "_name")]
        public string rName{ get; set; }
    

    【讨论】:

    • 在反序列化后,我将 jsondata(输出)转换为 Appointment 对象类型。所以 r 是这里的约会实例。 rName 是 Appointment 类中的 Name。
    • 你是对的。类必须与 json 匹配,我还添加了 JsonProperty 注释。但问题仍然是一样的,它必须是别的东西..还是谢谢你。
    • 任何机会你都可以投票给我 - 我需要信誉,这样我就可以对帖子发表评论,而不必发布答案:-D
    【解决方案2】:
    parameter pass appointment not jsondata in severside code
    
            public static string save(string appointment)
            {
                var output = JsonConvert.DeserializeObject<Appointment>(appointment);
                Appointment r = (Appointment)output;
                r.Name= output.rName.ToString();
                //sql code here     
                return r.Name;
            } 
    

    【讨论】:

      【解决方案3】:
      Try this
      $.ajax({
                  type: 'POST',
                  url: '../saveAppointment.aspx/save',
                  data: "{ 'appointment': "+JSON.stringify(appointment)+" }",//use inverted commas here
      
                  contentType: 'application/json; charset=utf-8',
                  dataType: 'json',
                  async: true,
      
                  success: function (result) {
                      alert(result);
                  },
                  error: function () {
                      alert('Error!' + result.d);
                  }
              });
      

      【讨论】:

      • 您能否告诉我们您所做的具体更改,以便将来的读者受益
      猜你喜欢
      • 2013-08-02
      • 2022-01-13
      • 1970-01-01
      • 2012-04-20
      • 2013-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多