我看了很多像这样的问题,我尝试了很多方法来解决这个问题,但我做不到。
var kullaniciAdi = '<%= Session["kullanici"] %>';
alert(kullaniciAdi);
$.ajax({
url: "http://localhost:63000/GetInfoService.asmx/GetInfos",
type: "POST",
dataType:'json',
data: JSON.stringify({ kullaniciAdi: kullaniciAdi }),
contentType: "application/json; charset=windows-1254",
success: function (infos) {
var items = infos.d;
$jQuery.each(items, function (index, val) {
$("div#adSoyad").html('<h1>' + val[index].ad + " " + val[index].soyad + '</h1>');
$("div#fotograf").html('<img src="' + val[index].fotograf + '" class="img-responsive">');
});
},
error: function (e) {
alert(e.leader);
}
});
无论我做了什么,它都无法正常工作。我试图将索引转为0或删除它们没有任何工作。
这是我一直在打电话的服务:
public class GetInfoService : System.Web.Services.WebService
{
[WebMethod]
public List<kullaniciBilgileri> GetInfos(string kullaniciAd)
{
List<kullaniciBilgileri> infos = new List<kullaniciBilgileri>();
try
{
SqlConnection conn = new SqlConnection("Data Source=.\\SQLExpress; Initial Catalog=twitter;Integrated Security=SSPI");
if (conn.State == ConnectionState.Closed)
conn.Open();
SqlCommand cmd = new SqlCommand("SELECT ad,soyad,fotograf FROM tblKullanici WHERE kullaniciAdi = @kullaniciAd");
cmd.CommandType = CommandType.Text;
cmd.Connection = conn;
cmd.Parameters.AddWithValue("@kullaniciAd", kullaniciAd);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
var kullanici = new kullaniciBilgileri
{
ad = dr["ad"].ToString(),
soyad = dr["soyad"].ToString(),
fotograf = dr["fotograf"].ToString()
};
infos.Add(kullanici);
}
return infos;
}
catch (SoapException se)
{
throw se;
}
}
public class kullaniciBilgileri
{
public string ad { get; set; }
public string soyad { get; set; }
public string fotograf { get; set; }
}
}
有关此方法的更多信息,请登录和主页。也许我犯了错误。
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static string GetSessionValue(string kullaniciAd)
{
HttpContext.Current.Session["kullanici"] = kullaniciAd;
return kullaniciAd;
}
}
public partial class Home : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (HttpContext.Current.Session["kullanici"] == null)
{
Response.Redirect("Login.aspx");
}
}
}
抱歉我的英语:)