ajax初学初用

前端之家收集整理的这篇文章主要介绍了ajax初学初用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

今天刚开始接触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);
});
});

4、JS文件可以调用后台CS函数

function GetVoice() {
functionPage.VoiceMessageInitialize(getVoiceCallBack);
}

5、后台如是需要与前台交互,需要注册

protected void Page_Load(object sender,EventArgs e)
{
Ajax.Utility.RegisterTypeForAjax(typeof(functionPage));
}

6、后台函数若是需要被前台调用,也需要注册

#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;
}

7、JSON对象的使用,后台返回JSON数组到前台

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); } } } }

原文链接:https://www.f2er.com/ajax/166591.html

猜你在找的Ajax相关文章