gpt4 book ai didi

JQuery 分割逗号与 ashx 处理程序分隔

转载 作者:行者123 更新时间:2023-12-01 01:31:52 25 4
gpt4 key购买 nike

我在将逗号分隔的字符串拆分为数组时遇到问题。在我的 ashx 处理程序页面中,我的字符串如下所示:

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,123@aol.com,555 anywhere street,Los Angeles,California,44502,google.com,Hotel & Travel

最佳答案

这是代码的演示。

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

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

console.log(stringArray);

结果在这里:

[“8”、“我的公司”、“(702) 555-1212”、“123@aol.com”、“555 Anywhere Street”、“洛杉矶”、“加利福尼亚”、“44502"、"google.com"、"酒店和旅行"]

console.log(xm[5]);

输出:洛杉矶

如果成功时获得的数据是字符串,那么流程可能如下......

我认为您编写的代码中没有任何错误。

如果数据不是字符串,则

执行 var string = ""+ data; 将其转换为字符串。

关于JQuery 分割逗号与 ashx 处理程序分隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40968021/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com