我正在尝试使用Web方法在页面加载后加载一个HTML块。下面的代码返回一个对象,但是在成功时它不会将返回的HTML附加到div“tabs”。下面的“Callerdt”变量是从数据库加载的数据表。任何想法为什么它不会将HTML附加到div标签?你认为传递的HTML字符串太大了吗?如果您需要更多信息并提前致谢,请告诉我。
<script type="text/javascript">
$(document).ready(function () {
var p = document.getElementById('pn').value, u = document.getElementById('user').value, er = document.getElementById('error').value;
if (!(er == "true" && u == "false" && p == "false")) {
$.ajax({
type: "POST",
url: 'CallerPopup.aspx/LoadPatients',
data: JSON.stringify({ phone: p, user: u }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert(data);
$('#tabs').append(data);
}
});
}
});
</script>
[WebMethod]
public static string LoadPatients(string phone, string user)
{
var ret = "No Patients Found";
...
//generate html to display patient details
var sb = new StringBuilder();
string addr = "";
for (int x = 0; x < Callerdt.Rows.Count; x++)
{
addr = Callerdt.Rows[x]["Street"].ToString() + " " + Callerdt.Rows[x]["city"].ToString() + ", " + Callerdt.Rows[x]["State"].ToString() + " " + Callerdt.Rows[x]["ZipCode"].ToString();
sb.AppendFormat("<div class='tabs'><table>" +
"<tr><td class='title'><label>Name:</label></td><td>{0}</td></tr>" +
"<tr><td><label>DOB:</label></td><td>{1}</td></tr>" +
"<tr><td><label>Address:</label></td><td>{2}</td></tr>" +
"<tr><td><label>SSN:</label></td><td>{3}</td></tr>" +
"<tr><td><label>Z Number:</label></td><td>{4}</td></tr>" +
"</table></div><br/>", Callerdt.Rows[x]["Name"].ToString(), Callerdt.Rows[x]["DOB"].ToString(), addr, Callerdt.Rows[x]["SSN"].ToString(), Callerdt.Rows[x]["ZNUM"].ToString());
}
ret = sb.ToString();
}
return ret;
}
答案 0 :(得分:0)
使用此
$.ajax({
type: "POST",
url: 'CallerPopup.aspx/LoadPatients',
data: JSON.stringify({ phone: p, user: u }),
dataType: "html",
success: function (data) {
alert(data);
$('#tabs').append(data);
}
});
答案 1 :(得分:0)
感谢您的回复。我终于通过以下方式设法让它工作。我注意到在chrome中调试时返回了一个名为'd'的json对象。所以我尝试了以下内容,现在它工作得很好!
<script type="text/javascript">
$(document).ready(function () {
var p = document.getElementById('pn').value, u = document.getElementById('user').value, er = document.getElementById('error').value, un = document.getElementById('usernum').value;
if (!(er == "true")) {
$("#loading").show()
$.ajax({
type: "POST",
url: 'CallerPopup.aspx/LoadPatients',
data: JSON.stringify({ phone: p, user: u, usernum: un }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
$('#tabs').append(data.d);
},
complete: function () {
$("#loading").hide();
}
});
}
});
</script>