簡體   English   中英

無法將參數從 jquery ajax 傳遞到 C# 后面的代碼

[英]cannot pass parameters from jquery ajax to code behind c#

我面臨一個問題,當我通過 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
                     }, ....}

阿賈克斯功能:

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 函數中,對象投訴沒有得到任何值,而是顯示為空。 這是問題嗎,因為在 ajax 函數中數據的參數少於具有更多屬性的投訴對象。 任何人都可以提供幫助。 提前致謝。

這里的名字

{
                        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',而不是空。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM