作者:你不必害怕q因为我会发光 | 来源:互联网 | 2014-05-27 11:53
varXMLHttpReqfalse;//创建XMLHttpRequest对象(主函数不需改变)functioncreateXMLHttpRequest(){if(window.XMLHttpRequest){//Mozilla浏览器XMLHttpReqnewXMLHttpRequest();}elseif(windo
var XMLHttpReq = false;
// 创建
XMLHttpRequest
对象(主函数不需改变)
function createXMLHttpRequest(){
if (window.XMLHttpRequest) {
// Mozilla
浏览器
XMLHttpReq = new XMLHttpRequest();
}
else
if (window.ActiveXObject) { //
IE 浏览器
try {
XMLHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
XMLHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {
}
}
}
}
//第一步:创建一个XML的DOM对象,可用如下方法完成
function CreateDomDoc() //创建XML文档对象
{
var signatures =
["Msxml2.DOMDocument.5.0", "Msxml2.DOMDocument.4.0",
"Msxml2.DOMDocument.3.0", "Msxml2.DOMDocument",
"Microsoft.XmlDom"];
for (var i = 0; i <
signatures.length; i++) {
try {
var domDoc = new ActiveXObject(signatures[i]);
return domDoc;
}
catch (e) {
}
}
return null;
}
//第二步:从客户端取得数据写入XML,方法如下:
function CreateXml(doc){
var root =
doc.createElement("root");
var title =
doc.createElement("title");
title.text =
document.getElementByIdx("tbTitle").value;
root.appendChild(title);
var ToGrade =
doc.createElement("ToGrade");
ToGrade.text =
document.getElementByIdx("ddlToGrade").options[document.getElementByIdx("ddlToGrade").selectedIndex].value;
root.appendChild(ToGrade);
var cOntent=
doc.createElement("content");
content.text =
getEditorContents();
root.appendChild(content);
var BreviaryPic =
doc.createElement("BreviaryPic");
BreviaryPic.text =
document.getElementByIdx("BreviaryPicUrl").value;
root.appendChild(BreviaryPic);
var classID =
doc.createElement("classID");
classID.text =
chkCheckBoxChs("articleClass");
root.appendChild(classID);
//alert(classID.text);
doc.appendChild(root);
//alert(doc.xml);
return doc.xml;
}
//第三步 互相调用
//第四步 AJAX发送
function sendRequest(url, domDoc){
createXMLHttpRequest();
XMLHttpReq.Onreadystatechange=
processResponse;
XMLHttpReq.open("POST", url,
false);
XMLHttpReq.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded;"); //关键,否则出错
XMLHttpReq.send(domDoc);
//发送一定是domDoc文档对象,如果只发送xml将出错”statue=500”
}
//处理返回信息函数(处理xml格式的返回信息)
function processResponse(){
if (XMLHttpReq.readyState == 4)
{
if (XMLHttpReq.status == 200) {
info = XMLHttpReq.responseText;
//alert(info);
if (info == "1") {
ShowMsgo.show(msgTitle,msgContentSuccess, 2)
}
else{
ShowMsgo.show(msgTitle,msgContentFaild,
2)
}
}
else {
//alert("ajax调用失败!" + XMLHttpReq.status);
ShowMsgo.show(msgTitle,msgContentFaild,
2)
}
}
}
//建立doc
function CreateDomXml(){
var domDoc = CreateDomDoc();
//创建对象 将用send发送到服务器端
if (domDoc != null) {
var xml = CreateXml(domDoc); //写入xml 返回xml文档
//alert(domDoc);//
return domDoc;
}
else {
alert("未安装MSXML控件");
}
}
//页面执行方法
function sendto(){
if (document.form1.tbTitle.value
== "") {
alert("请填写文章标题");
document.form1.tbTitle.focus();
return false;
}
else
if (getEditorContents() == "") {
alert("请填写文章内容");
return false;
}
else
if (chkCheckBoxChs("articleClass") == "") {
alert("请选择文章发布类别");
return false;
}
else {
var url = "ajaxArticleAdd.aspx";