2016-12-05 46 views
3

我無法將逗號分隔的字符串拆分爲數組。在我的ashx處理程序頁面中,我的字符串如下所示:從ashx處理程序分隔的JQuery分隔逗號

context.Response.Write(string.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9} ", BusProfileID, BusinessName, BusinessPhone, BusinessEmail, BusinessAddress, BusinessCity, BusinessState, BusinessZip, BusinessWebsite, BusinessCategory)); 

當我嘗試執行數組時,結果不顯示。

<script> 
     $(document).ready(function() { 
      $('#ContentPlaceHolder1_businessSelect').change(function() { 
       $.ajax({ 
        contentType: "text/html; charset=utf-8", 
        data: "ID=" + $('#ContentPlaceHolder1_businessSelect').val(), 
        url: "getBusValue.ashx", 
        dataType: "text", 
        success: function (data) { 
        var vardata = JSON.stringify(data) 
        var arr = vardata.split(',') 
        $("#ContentPlaceHolder1_BusProfileID").val(arr[0]); 
        $("#ContentPlaceHolder1_BusinessName").val(arr[1]; 
        $("#ContentPlaceHolder1_BusinessPhone").val(arr[2]); 
        $("#ContentPlaceHolder1_BusinessEmail").val(arr[3]); 
        $("#ContentPlaceHolder1_BusinessAddress").val(arr[4]); 
        $("#ContentPlaceHolder1_BusinessCity").val(arr[5]); 
        $("#ContentPlaceHolder1_BusinessState").val(arr[6]).prop('selected',true); 
        $("#ContentPlaceHolder1_BusinessZip").val(arr[7]); 
        $("#ContentPlaceHolder1_BusinessWebsite").val(arr[8]); 
        $("#ContentPlaceHolder1_BusinessCategory").val(arr[9]).prop('selected', true); 
        } 
       }); 
      }); 
     }); 
    </script> 

這是我的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, BusinessAddress, BusinessCity, BusinessState, BusinessZip, BusinessWebsite, BusinessCategory FROM [BusProfile] WHERE BusinessName = @BusinessName", conn); 
     comm.Parameters.Add("@BusinessName", System.Data.SqlDbType.VarChar); 
     comm.Parameters["@BusinessName"].Value = ID; 
     try 
     { 
      conn.Open(); 
      reader = comm.ExecuteReader(); 
      if (reader.Read()) 
      { 
       string BusProfileID = reader["BusProfileID"].ToString(); 
       string BusinessName = reader["BusinessName"].ToString(); 
       string BusinessPhone = reader["BusinessPhone"].ToString(); 
       string BusinessEmail = reader["BusinessEmail"].ToString(); 
       string BusinessAddress = reader["BusinessAddress"].ToString(); 
       string BusinessCity = reader["BusinessCity"].ToString(); 
       string BusinessState = reader["BusinessState"].ToString(); 
       string BusinessZip = reader["BusinessZip"].ToString(); 
       string BusinessWebsite = reader["BusinessWebsite"].ToString(); 
       string BusinessCategory = reader["BusinessCategory"].ToString(); 

       context.Response.Write(string.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9} ", BusProfileID, BusinessName, BusinessPhone, BusinessEmail, BusinessAddress, BusinessCity, BusinessState, BusinessZip, BusinessWebsite, BusinessCategory)); 
      } 
      reader.Close(); 
     } 

     finally 
     { 
      conn.Close(); 
     } 
    } 

這是數據怎麼看起來像在成功的文本框:

8,My Business Inc,(702) 555-1212,[email protected],555 anywhere street,Los Angeles,California,44502,google.com,Hotel & Travel 
+1

你可以顯示語句的結果'var arr = data.split(',')'? – Satpal

+0

您只是將整個字符串寫入元素... –

+0

逗號將它們分隔爲@Satpal的註釋,請使用data.split(',')來執行此操作。 –

回答

0

這是一個lenthy職位,但我希望它能幫助您。

第一部分是使用兩種不同方式的Web服務,一種是作爲Web服務的方式,另一種是作爲列表並將其序列化的常規類。

webservice1.asmx:

namespace WebApplication1 
    { // a class in context with how the data are is being used 
     [Serializable] 
     public class select2 
     { 
      public String id { get; set; } 
      public String text { get; set; } 
      public select2(String code, String name) 
      { 
       this.id = code; 
       this.text = name; 
      } 
     } 
     // input parms not used but to show concept 
     public struct parameters 
     { 
      string parm1; 
      string parm2; 
     } 

     /// <summary> 
     /// Summary description for WebService1 
     /// </summary> 
     [WebService(Namespace = "http://tempuri.org/")] 
     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
     [System.ComponentModel.ToolboxItem(false)] 
     // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
     [System.Web.Script.Services.ScriptService] 
     public class WebService1 : System.Web.Services.WebService 
     { 

      [WebMethod] 
      public string GetAList(String s) 
      { 
       System.Web.Script.Serialization.JavaScriptSerializer ser = new System.Web.Script.Serialization.JavaScriptSerializer(); 
       parameters parms = ser.Deserialize<parameters>(s); 
       return makelist(); 
      } 

      // return a list of serialized codes and countries 
      public String makelist() 
      { 
       List<select2> list = new List<select2>(); 

       list.Add(new select2("AI", "Anguilla")); 
       list.Add(new select2("AQ", "Antarctica")); 
       list.Add(new select2("AG", "Antigua and Barbuda")); 
       list.Add(new select2("AR", "Argentina")); 
       list.Add(new select2("AM", "Armenia")); 
       list.Add(new select2("AW", "Aruba")); 
       list.Add(new select2("AU", "Australia")); 
       list.Add(new select2("AT", "Austria")); 
       list.Add(new select2("AZ", "Azerbaijan")); 
       list.Add(new select2("BS", "Bahamas")); 
       list.Add(new select2("BH", "Bahrain")); 
       list.Add(new select2("BD", "Bangladesh")); 
       list.Add(new select2("BB", "Barbados")); 
       list.Add(new select2("BY", "Belarus")); 
       list.Add(new select2("BE", "Belgium")); 
       list.Add(new select2("BZ", "Belize")); 
       // did it this way to show you which to use 
       System.Web.Script.Serialization.JavaScriptSerializer ser = new System.Web.Script.Serialization.JavaScriptSerializer(); 
       String jsonList = ser.Serialize(list); 
       return jsonList; 
      } 
     } 
    } 

下一個部分是後面部分的代碼:用於填充一個選擇2盒從一個AJAX調用Web方法

namespace WebApplication1 
    { 

     public partial class select2 : System.Web.UI.Page 
     { 
      public String JsonCountries { get 
      { 
        // use the web service as a regular class to return the serialized list 
       return (new WebService1()).makelist(); 
      } } 

      protected void Page_Load(object sender, EventArgs e) 
      { 

      } 
     } 
    } 

最後的aspx頁面和第二個選擇2是由後面的代碼放置的數據填充的。這與你所做的有點不同,但是概念的證明已經到位了,應該給你足夠的經驗。

 $(document).ready(
       function() { 
        // property on code behnd 
        var cblist = <%=JsonCountries%>; 
        $("#sel3").select2({ data:cblist }); 

        parms = JSON.stringify({ parm1: "one", parm2: "two" }); 

         $.ajax({ 
          url: "WebService1.asmx/GetAList", 
          data: JSON.stringify({s:parms}), 
          type: 'post', 
          contentType: "application/json; charset=utf-8", 
          dataType: "json", 

          success: function (data, status) { 
           var contries = JSON.parse(data.d); 
           $("#sel2").select2({ data: contries }); 
          }, 
          error: function (one, two) { 
           debugger; 
          } 
         }); 

       } 
      ); 
     </script> 
    </head> 
     <body> 
      <div> 
       <p>populated from ajax</p> 
       <select id="sel2"></select> 
      </div> 

      <div> 
       <p>populated from code behind</p> 
       <select id="sel3"></select> 
      </div> 
     </body> 
    </html> 
1

下面是代碼的演示。

var string = "8,my business inc,(702) 555-1212,[email protected],555 anywhere street,Los Angeles,california,44502,google.com,hotel and ttravel"; 

var stringArray = string.split(','); 

console.log(stringArray); 

其結果是在這裏:

[ 「8」, 「我的商業公司」, 「(702)555-1212」, 「[email protected]」,「555隨地街道」, 「洛杉磯」, 「加州」, 「44502」, 「google.com」, 「酒店和ttravel」]

console.log(xm[5]); 

輸出: 洛杉磯

如果你成功的數據是一個字符串,然後可能這裏是流如何去.....

我不認爲這是在你寫的代碼的任何錯誤。

如果數據不是字符串,然後

var string = "" + data;將其轉換爲一個字符串。

+1

我向代碼中添加了var vardata = JSON.stringify(數據),它現在可以工作。 –

+0

可能因爲它不是字符串.... –

+0

在這種情況下,JSON.Stringify(數據)只是將數據轉換爲字符串數據類型。 –