2016-12-22 38 views
1

在Google上搜索並閱讀文檔後,我很困惑它是如何工作的。當調用具有值的通用處理程序併成功返回多個值時。你如何循環訪問數據庫中的記錄?你是循環遍歷通用處理程序的記錄,還是通過JQuery的每個函數成功執行?以下是當前的代碼,當存在多個值時不起作用。刪除$。每個(VARDATA,函數()和它的作品;但是,只有一條記錄顯示JQuery從通用處理程序中檢索多個值

這是該數據應如何照顧你在伊利在文本框中鍵入並單擊按鈕

Business Profile ID: 8 
Business Name: The Boston Store 
Phone Number: 814-455-1478 
E-Mail: [email protected] 

Business Profile ID: 9 
Business Name: Sam The Man Pizza 
Phone Number: 814-868-3809 
E-Mail: [email protected] 

jQuery腳本

$(document).ready(function() { 
     $('#button').click(function() { 
      $.ajax({ 
       contentType: "text/html; charset=utf-8", 
       data: "ID=" + $('#businessSelect').val(), 
       url: "getTest.ashx", 
       dataType: "text", 
       success: function (data) { 
        var vardata = JSON.parse(data); 
        $.each(vardata, function (index, value) { 
         $("#BusProfileID").html(value.BusProfileID); 
         $("#BusinessName").html(value.BusinessName); 
         $("#BusinessPhone").html(value.BusinessPhone); 
         $("#BusinessEmail").html(value.BusinessEmail); 
        }); 
       } 
      }) 
     }); 
    }); 

ashx的處理程序頁面

 public void ProcessRequest(HttpContext context) 
    { 
     context.Response.ContentType = "text/html"; 
     string ID = context.Request.QueryString["ID"]; 
     SqlConnection conn; 
     SqlCommand comm; 
     SqlDataReader reader; 
     string connectionString = ConfigurationManager.ConnectionStrings["Conn"].ConnectionString; 
     conn = new SqlConnection(connectionString); 
     comm = new SqlCommand("SELECT BusProfileID, BusinessName, BusinessPhone, BusinessEmail FROM [BusProfile] WHERE BusinessCity = @BusinessCity", conn); 
     comm.Parameters.Add("@BusinessCity", System.Data.SqlDbType.VarChar); 
     comm.Parameters["@BusinessCity"].Value = ID; 
     try 
     { 
      conn.Open(); 
      reader = comm.ExecuteReader(); 
      List<BusinessData> objList = new List<BusinessData>(); 
      BusinessData objData; 
      while (reader.Read()) 
      { 
       objData = new BusinessData(); 
       objData.BusProfileID = reader["BusProfileID"].ToString(); 
       objData.BusinessName = reader["BusinessName"].ToString(); 
       objData.BusinessPhone = reader["BusinessPhone"].ToString(); 
       objData.BusinessEmail = reader["BusinessEmail"].ToString(); 
       objList.Add(objData); 
       context.Response.Write(JsonConvert.SerializeObject(objList)); 
      } 
      reader.Close(); 
     } 

     finally 
     { 
      conn.Close(); 
     } 
    } 

    public class BusinessData 
    { 
     public string BusProfileID { get; set; } 
     public string BusinessName { get; set; } 
     public string BusinessPhone { get; set; } 
     public string BusinessEmail { get; set; } 
    } 
    public bool IsReusable 
    { 
     get 
     { 
      return false; 
     } 
    } 
} 

aspx頁面

  <div class="row"> 
       <div class="columns medium-12"> 
        <div class="row"> 
         <div class="medium-6 columns medium-centered"> 
          <label style="font-size:1em">Select City:</label> 
          <input type="text" id="businessSelect" style="height:2em;" /> <input type="button" id="button" value="Click me" /> 
         </div> 
        </div> 
        <div class="row"> 
         <div class="medium-6 columns medium-centered"> 
          <label style="font-size:1em">Business Profile ID:</label> 
          <label id="BusProfileID" style="font-size:1em;"></label> 
         </div> 
        </div> 
        <div class="row"> 
         <div class="medium-6 columns medium-centered"> 
          <label style="font-size:1em">Business Name:</label> 
          <label id="BusinessName" style="font-size:1em;"></label> 
         </div> 
        </div> 
        <div class="row"> 
         <div class="medium-6 columns medium-centered"> 
          <label style="font-size:1em">Phone Number:</label> 
          <label id="BusinessPhone" style="font-size:1em;"></label> 
         </div> 
        </div> 
        <div class="row"> 
         <div class="medium-6 columns medium-centered"> 
          <label id="BusinessEmail" style="font-size:1em">E-Mail:</label> 
         </div> 
        </div> 
       </div> 

回答

0

如果通過結果要loop,它必須是一個object。現在你是stringify你的結果。所以你不能迭代它。

使用JSON.parse的結果轉換爲JSON對象,然後使用$.each該對象

var vardata = JSON.parse(data); 
$.each(vardata, function (index, value) { 
    // TODO 
}); 

上所以你不會需要拆分並考慮到array。您可以參考each聲明中的value參數。

編輯

要從JSON使用的值,你可以使用其屬性,從服務器的結果集。

一樣,

$.each(vardata, function (index, value) { 
    $("#BusProfileID").html(value.BusProfileID); 
    $("#BusinessName").html(value.BusinessName); 
    $("#BusinessPhone").html(value.BusinessPhone); 
    $("#BusinessEmail").html(value.BusinessEmail); 
}); 

使用class,讓你從SQL Query

public class BusinessData 
{ 
    public long BusProfileID { get; set; } 
    public string BusProfileID { get; set; } 
    public string BusProfileID { get; set; } 
    public string BusProfileID { get; set; } 
} 

獲取的數據創建一個List<BusinessData>對象,並從你的讀者添加每個項目到這個列表

​​ 包含

JsonConvertNewtonsoft.Json nuget包中。

+0

它不返回任何結果。我編輯了原始問題以顯示您的建議。 –

+0

我還沒有根據您的其他建議獲得任何結果。但是,通過解決這個問題,我正在更好地理解JSON的工作原理。 –