【问题标题】:cannot pass parameters from jquery ajax to code behind c#无法将参数从 jquery ajax 传递到 c# 后面的代码
【发布时间】:2017-07-01 02:51:17
【问题描述】:

我面临一个问题,当我通过 jquery ajax 传递测试框值时,我看不到后面代码中的值。

function CreateComplaint(){

 var category       = $('#ddlCompCategory') .val();
 var name           = $('#txtCompUserName') .val();
 var subject        = $('#txtCompSub')      .val();
 var mobile         = $('#txtCompMobileNo') .val();
 var address        = $('#txtaCompAddr')    .val();
 var email          = $('#txtCompEmail')    .val();


$('#spinnerAddComplaint').show();
 CreateComplaintXHR(
                     '/api/Complaints/CreateComplaint', 
                     {
                        Token       : RBApp.Token,
                        CategoryId  : category,
                        UName       : name,
                        Subject     : subject,
                        Mobile      : mobile,
                        Email       : email,
                        Address     : address
                     }, ....}

ajax 函数:

function CreateComplaintXHR(url, data, success, error, alWaysCallback) {
  $.ajax({
      url: url,
      type: 'POST',
      dataType: 'json',
      data: data,
      success: function (data, textStatus, jqXHR) {

          if (success)
              success(data, textStatus, jqXHR);
      },
      error: function (jqXHR, textStatus, errorThrown) {

          if (error)
              error(jqXHR, textStatus, errorThrown);
      }
  }).always(function () {

  });

  }

后面的代码:

 [HttpPost]
    [ActionName ( "CreateComplaint" )]
    public ResponseModel InsertComplaintMethod(InsertComplaint Complaint)
    {
        log .Debug ( "Create Complaint request received : " );
        log .Debug ( "Request params : " + Complaint .Serialize() );

        User user = loadUser ( Complaint .Token );

        ComplaintsAdapter adapter = new ComplaintsAdapter ( user );

        ResponseModel response = adapter .CreateComplaintOrSuggestion ( Complaint );



        return response;
    }

插入投诉

public class InsertComplaint
{
    //public string Token { get; set; }
    public string Lat = "0"; //{ get; set; }
    public string Lng = "0"; //{ get; set; }

    public string deviceID = "0";
    public string complaintSubject { get; set; }
    public string complaintText { get; set; }

    public string name    { get; set; }
    public string phoneNumber { get; set; }
    public string attachements = null;


    }

在 insertComplaintMethod 函数对象中,投诉没有得到任何值,而是显示为 null。这是问题吗,因为在 ajax 函数数据中的参数比在具有更多属性的投诉对象中少。任何人都可以提供帮助。 提前致谢。

【问题讨论】:

    标签: c# jquery ajax web-services rest


    【解决方案1】:

    这里的名字

    {
                            Token       : 'test',
                            CategoryId  : 'test',
                            UName       : 'test',
                            Subject     : 'test',
                            Mobile      : 'test',
                            Email       : 'test',
                            Address     : 'test'
    }
    

    这里

    public class InsertComplaint
    {
        //public string Token { get; set; }
        public string Lat = "0"; //{ get; set; }
        public string Lng = "0"; //{ get; set; }
        public string deviceID = "0";
        public string complaintSubject { get; set; }
        public string complaintText { get; set; }
        public string name    { get; set; }
        public string phoneNumber { get; set; }
        public string attachements = null;
    }
    

    应该是一样的。例如替换这一行

     Mobile : 'test',
    

    这个

     phoneNumber : '123'
    

    你会看到“123”,而不是空值。

    【讨论】:

    • 谢谢哥们。我花了 2 天时间来解决问题。有用。再次感谢。
    猜你喜欢
    • 1970-01-01
    • 2013-03-19
    • 1970-01-01
    • 2012-06-22
    • 2011-10-23
    • 2014-04-05
    • 1970-01-01
    • 2020-08-31
    • 1970-01-01
    相关资源
    最近更新 更多