您的位置:首页 > Web前端 > JavaScript

javascript post数据到WCF反馈json或者XML

2009-04-06 13:26 309 查看
前端代码:

<script language="javascript" type="text/javascript">
//创建XMLHTTP
function createXMLHTTP(){
var httpRequest;
try {
httpRequest = new XMLHttpRequest();
}
catch (e) {
try {
httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {
return null;
}
}
}
return httpRequest;
}
//构造XML格式的文本
function createXML(){
var id = document.getElementById("id").value;
var title = document.getElementById("title").value;
var content = document.getElementById("content").value;
var xml = '<?xml version="1.0" encoding="utf-8" ?>';
xml+='<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">';
xml+='<soap:Body>';
xml+='<XmlService xmlns="http://tempUri.org/">';
//var xml = '';
xml+='<id type="number">'+id+'</id>';
xml+='<mytitle type="string">'+title+'</mytitle>';
xml+='<mycontent type="string">'+content+'</mycontent>';
xml+='</XmlService></soap:Body></soap:Envelope>';
return xml;
}
//构造JSON字符串
function createJSON(){
var id = document.getElementById("id").value;
var title = document.getElementById("title").value;
var content = document.getElementById("content").value;
var json = '{"id":'+id+',"mytitle":"'+title+'","mycontent":"'+content+'"}';
return json;
}
//异步调用服务器
function callServer(postType) {
var xmlHttp =createXMLHTTP();
if (xmlHttp == null) {alert('浏览器不支持ajax');return;}
xmlHttp.onreadystatechange = function(){
if (xmlHttp.readyState == 4) {callBack(xmlHttp.responseText)}
}
var body;
var contentType;
var url;
if(postType == 'json'){
body = createJSON();
contentType = "application/json";
url = "/JsonXmlService.svc/JsonService";
}
if(postType == 'xml'){
body = createXML();
contentType = "text/xml";
url = "/JsonXmlService.svc/XmlService";
}
xmlHttp.open("POST", url, true);
xmlHttp.setRequestHeader("Content-type", contentType);
xmlHttp.send(body);
}
//回调事件
function callBack(responseText){
alert(responseText);
}
</script>

前端还有几个HTML控件

<div>
文章标题:<input type="text" id="title" /><br />
文章内容:<textarea rows="" cols="" id="content"></textarea>
<input type="hidden" id="id" value="1" /><br />
<input type="button" value="以JSON方式提交" onclick="callServer('json');" />
<input type="button" value="以XML方式提交" onclick="callServer('xml');" />
</div>
后端代码

using System;
using System.Text;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.ServiceModel.Channels;
using System.Xml.Serialization;
using System.Xml;
using System.IO;
namespace JsonXmlWcf
{
[ServiceContract(Namespace = "")]
public interface IJsonXmlService
{
[OperationContract]
[WebInvoke(ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
MyDataType JsonService(int id, string mytitle, string mycontent);
[OperationContract(Action="*")]
[WebInvoke(ResponseFormat = WebMessageFormat.Xml, RequestFormat=WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
Message XmlService(Message m);
}
public class JsonXmlService:IJsonXmlService
{
public MyDataType JsonService(int id,string mytitle,string mycontent)
{
return MyService(id, mytitle, mycontent, "以JSON形式传递");
}
public Message XmlService(Message m)
{
return m;
}
private MyDataType MyService(int id,string mytitle,string mycontent,string info)
{
MyDataType MyData = new MyDataType();
MyData.Id = id;
MyData.MyTitle = string.Format("{0}({1})", mytitle,info);
MyData.MyContent = mycontent;
return MyData;
}
}
[DataContract]
public class MyDataType
{
private int _id;
private string _mytitle;
private string _content;
[DataMember]
public int Id
{
get { return _id; }
set { _id = value; }
}
[DataMember]
public string MyTitle
{
get { return _mytitle; }
set { _mytitle = value; }
}
[DataMember]
public string MyContent
{
get { return _content; }
set { _content = value; }
}
}

}

遗留问题
messagecontract
WCF是怎么序列化,反序列化消息的
一切消息的基类Message的成员

总结:这样搞WCF不是方法,应该系统的学医下

代码:http://files.cnblogs.com/liulun/JsonXmlWcf.rar
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐