今天刚开始接触ajax,好烦啊,不会用,搞了一大天了,记下一些小细节问题吧
1、记得引用js库
<script src="../js/jquery.min.js" type="text/javascript"></script>
2、记得引入你的JS文件
<script src="JS/Voice.js" type="text/javascript"></script>
3、JS里面页面加载的注册事件必须写到$(document).ready(function ()里面
$(document).ready(function () {
$(".voiceMessage").click(function () {
GetVoice();
});
$("#inputSend").click(function () {
var msg = document,getElementById("message").innerHTML;
alert(msg);
SentVoice(msg);
});
});
function GetVoice() {
functionPage.VoiceMessageInitialize(getVoiceCallBack);
}
protected void Page_Load(object sender,EventArgs e)
{
Ajax.Utility.RegisterTypeForAjax(typeof(functionPage));
}
#region zq-语音短信;报警信息
[Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
public string VoiceMessageInitialize()
{
//deviceID = Convert.ToInt32(Session["deviceID"].ToString());
deviceID = 16510;
PersonService personService = new PersonService();
List<Voice> voiceList = new List<Voice>();
voiceList = personService.GetDeviceVoice(deviceID).ToList<Voice>();
string res = JSONHelper.Serialize(voiceList);
return res;
}
function getVoiceCallBack(res) { if (res.value == null) { return; } else { var strJSON = res.value; //得到的JSON var obj = eval("(" + strJSON + ")"); //转换后的JSON对象 for (i in obj) { if (i < 5) { var DateSpan = "dateSpan" + i; var sentDate = obj[i].Send_Date; var ContentSpan = "contentSpan" + i; var content = obj[i].Content; var msgli = "li" + i; changeMessage(DateSpan,sentDate,ContentSpan,content,msgli); } } } }