最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

javascript與webservice的通信實(shí)現(xiàn)代碼

 更新時(shí)間:2010年12月25日 23:16:24   作者:  
關(guān)于javascript與webservice的通信,從理論上來(lái)說(shuō)實(shí)現(xiàn)應(yīng)該不難,主要是將服務(wù)器端的xml數(shù)據(jù)進(jìn)行一個(gè)簡(jiǎn)單的處理然后以一種適當(dāng)?shù)男问秸宫F(xiàn)成來(lái)。
在我這里,我選擇將xml直接轉(zhuǎn)換為json,以便后續(xù)javascript應(yīng)用的處理。我使用.net平臺(tái)構(gòu)建簡(jiǎn)單的webservice。
Request.asmx
復(fù)制代碼 代碼如下:

using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Drawing;
using System.Drawing.Imaging;
namespace NightKidsServices
{
/// <summary>
/// Service1 的摘要說(shuō)明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
public class TestService :WebService
{
private static int picNum = -1;
[WebMethod]
public Resource GetResource()
{
return Resource.CreateResource("pic2", "asdfasd", 0);
}
[WebMethod]
public string HelloWorld()
{
return "Hello";
}
[WebMethod]
public byte[] GetPic()
{
picNum = (picNum + 1) % 32;
Image image = Image.FromFile(this.Server.MapPath("jpeg/" + (picNum+1).ToString() + ".bmp"));
MemoryStream mem=new MemoryStream();
image.Save(mem, ImageFormat.Jpeg);
return mem.GetBuffer();
}
[WebMethod]
public List<Resource> GetResourceList()
{
return new List<Resource>(new Resource[] { Resource.CreateResource("pic1", "jpeg/1.bmp", 0),Resource.CreateResource("pic2", "jepg/2.bmp", 0), Resource.CreateResource("pic3", "jpeg/3.bmp", 0), Resource.CreateResource("pic4", "jepg/4.bmp", 0) });
}
}

以上只是一個(gè)簡(jiǎn)單的測(cè)試使用,便于后續(xù)使用javascript處理不同類型的數(shù)據(jù)
對(duì)于javascript,肯定是使用xmlhttprequest對(duì)象來(lái)訪問(wèn)服務(wù)器端的,不過(guò)這里為了簡(jiǎn)單,我沒(méi)有考慮兼容性問(wèn)題,直接使用xmlhttprequest對(duì)象(我使用chrome瀏覽器作為測(cè)試瀏覽器),為此我使用AjaxClient類來(lái)進(jìn)行http操作(Post 方法),WebService類來(lái)封裝處理webservice(調(diào)用AjaxClient類作為操作類),JsonConverter類處理xml數(shù)據(jù)轉(zhuǎn)換為json數(shù)據(jù)
common.js(包含JsonConverter類)
復(fù)制代碼 代碼如下:

// JavaScript Document
function $(id)
{
return document.getElementById(id);
}
function GetXmlHttp()
{
if(window.XMLHttpRequest)
return new XMLHttpRequest();
}
var JsonConverter={};
JsonConverter.FlagStack=[];
JsonConverter.ConvertFromXML=function(xmlRootNode)
{
if(!xmlRootNode)
return;
var converter={};
converter.render=function(node,isArrayElement)
{
var returnStr='';
var isArray=false;
if(node.childNodes.length==1)
{
returnStr+=node.nodeName+':' + "'" + node.firstChild.nodeValue + "'" ;
if(node==xmlRootNode)
returnStr='{' + returnStr + '}';
return returnStr;
}
isOneNode=false;
if(node.nodeName.match("ArrayOf*"))
isArray=true;
if(isArray)
returnStr+='[';
else
{
returnStr+='{';
if(!(isArrayElement || xmlRootNode==node))
returnStr=node.nodeName + ':' + returnStr;
}
for(var i=1;i<node.childNodes.length;i+=2)
{
returnStr+=this.render(node.childNodes[i],isArray) + ',';
}
returnStr=returnStr.slice(0,-1);
if(isArray)
returnStr+=']';
else
returnStr+='}';
return returnStr;
}
//alert(converter.render(xmlRootNode));
return eval('(' + converter.render(xmlRootNode) + ')');
}

<SPAN style="FONT-FAMILY: verdana, 'courier new'"><SPAN style="FONT-SIZE: 14px; LINE-HEIGHT: 21px; WHITE-SPACE: normal"><BR></SPAN></SPAN>
AjaxClient.js
復(fù)制代碼 代碼如下:

// JavaScript Document
function AjaxClient(url)
{
var xmlhttp=GetXmlHttp();
var request_url=url;
var msgList=new Array();
var isOpen=false;
var isRunning=false;
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==xmlhttp.DONE)
{
isRunning=false;
if (xmlhttp.status==200)
{
msgList.push(xmlhttp.responseXML);
}
}
}
this.Open=function()
{
if(xmlhttp==null)
xmlhttp=GetXmlHttp();
isOpen=true;
if(xmlhttp==null)
isOpen=false;
}
this.Send=function(msg)
{
if (isOpen)
{
xmlhttp.open("POST",request_url,false);
//alert(request_url);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-length",msg==null?0:msg.length);
//xmlhttp.setRequestHeader("Keep-Alive","ON");
xmlhttp.send(msg==null?"":msg);
isRunning=true;
}
}
this.GetUrl=function()
{
return request_url.length==0?'':request_url;
}
this.SetUrl=function(url)
{
request_url=url;
}
this.Receive=function()
{
var num=0;
while(!msgList.length)
{
num++;
if (num>=20000)
break;
}
return msgList.length==0?null:msgList.shift();
}
this.Close=function()
{
if(!isRunning)
{
isOpen=false;
xmlhttp=null;
}
}
}

WebService.js
復(fù)制代碼 代碼如下:

// JavaScript Document
function WebService(url)
{
var ajaxclient=new AjaxClient(null);
var requestUrl=url;
var responseMsg=null;
this.Request=function(requestName,paraList)
{
var url=requestUrl +'/' + requestName;
var sendData='';
ajaxclient.SetUrl(url);
ajaxclient.Open();
//alert(ajaxclient.GetUrl());
if (paraList!=null)
{
for(var obj in paraList)
sendData+=obj.toString() + '=' + paraList[obj] + '&';
sendData=sendData.slice(0,-1);
}
ajaxclient.Send(sendData);
//ajaxclient.Close();
//responseMsg=XMLtoJSON(ajaxclient.Receive());
//for(var obj in responseMsg)
// alert(obj.toString() + ':' + responseMsg[obj].toString());
responseMsg=ajaxclient.Receive();
}
this.GetResponse=function()
{
return responseMsg;
}
}

調(diào)用很簡(jiǎn)單,只需
復(fù)制代碼 代碼如下:

var webService=new WebService('http://localhost/NightKidsWebService/Request.asmx');
webService.Request("GetResourceList",null);
alert(JsonConverter.ConvertFromXML(webService.GetResponse().firstChild));

在Request方法里面的第一個(gè)參數(shù)對(duì)應(yīng)不同的服務(wù)名稱,第二個(gè)參數(shù)加入對(duì)應(yīng)的服務(wù)的參數(shù)表(json格式,例如:{id:123,name:'nightKids'})

相關(guān)文章

  • JavaScript中內(nèi)存泄漏的介紹與教程(推薦)

    JavaScript中內(nèi)存泄漏的介紹與教程(推薦)

    內(nèi)存泄露是指一塊被分配的內(nèi)存既不能使用,又不能回收,直到瀏覽器進(jìn)程結(jié)束。下面這篇文章主要給的大家介紹了關(guān)于JavaScript中內(nèi)存泄漏的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。
    2017-06-06
  • js格式化時(shí)間的簡(jiǎn)單實(shí)例

    js格式化時(shí)間的簡(jiǎn)單實(shí)例

    本文分享了js格式化時(shí)間的實(shí)例代碼,需要的朋友可以看下
    2016-11-11
  • JavaScript Array對(duì)象詳解

    JavaScript Array對(duì)象詳解

    這篇文章主要為大家詳細(xì)介紹了JavaScript function函數(shù)種類,知識(shí)點(diǎn)很全面,包括普通函數(shù)、匿名函數(shù)、閉包函數(shù),感興趣的小伙伴們可以參考一下
    2016-03-03
  • 微信小程序 checkbox使用實(shí)例解析

    微信小程序 checkbox使用實(shí)例解析

    這篇文章主要介紹了微信小程序 checkbox使用實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-09-09
  • js兼容火狐獲取圖片寬和高的方法

    js兼容火狐獲取圖片寬和高的方法

    這篇文章主要介紹了js兼容火狐獲取圖片寬和高的方法,涉及javascript圖片操作的相關(guān)技巧,需要的朋友可以參考下
    2015-05-05
  • Javascript 創(chuàng)建類并動(dòng)態(tài)添加屬性及方法的簡(jiǎn)單實(shí)現(xiàn)

    Javascript 創(chuàng)建類并動(dòng)態(tài)添加屬性及方法的簡(jiǎn)單實(shí)現(xiàn)

    下面小編就為大家?guī)?lái)一篇Javascript 創(chuàng)建類并動(dòng)態(tài)添加屬性及方法的簡(jiǎn)單實(shí)現(xiàn)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-10-10
  • javascript實(shí)現(xiàn)文件拖拽事件

    javascript實(shí)現(xiàn)文件拖拽事件

    這篇文章主要為大家詳細(xì)介紹了javascript實(shí)現(xiàn)文件拖拽事件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • JS實(shí)現(xiàn)的頁(yè)面自定義滾動(dòng)條效果

    JS實(shí)現(xiàn)的頁(yè)面自定義滾動(dòng)條效果

    這篇文章主要介紹了JS實(shí)現(xiàn)的頁(yè)面自定義滾動(dòng)條效果,涉及JavaScript結(jié)合css設(shè)置頁(yè)面滾動(dòng)條樣式的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-10-10
  • JavaScript設(shè)計(jì)模式之代理模式介紹

    JavaScript設(shè)計(jì)模式之代理模式介紹

    這篇文章主要介紹了JavaScript設(shè)計(jì)模式之代理模式介紹,代理模式顧名思義就是用一個(gè)類來(lái)代替另一個(gè)類來(lái)執(zhí)行方法功能,需要的朋友可以參考下
    2014-12-12
  • iframe中子父類窗口調(diào)用JS的方法及注意事項(xiàng)

    iframe中子父類窗口調(diào)用JS的方法及注意事項(xiàng)

    本文給大家介紹iframe中子父類窗口調(diào)用JS的方法及注意事項(xiàng),介紹的超詳細(xì),需要的朋友快來(lái)學(xué)習(xí)下
    2015-08-08

最新評(píng)論

甘洛县| 什邡市| 古田县| 新津县| 九龙县| 定日县| 启东市| 巫溪县| 资中县| 长泰县| 株洲市| 沙田区| 兰考县| 曲周县| 天等县| 嘉善县| 安徽省| 赤水市| 六安市| 汪清县| 沙河市| 濉溪县| 宜良县| 镇巴县| 德阳市| 兴山县| 金沙县| 阜平县| 新建县| 双流县| 遵化市| 广西| 友谊县| 安塞县| 吉木乃县| 罗城| 兴安县| 汉寿县| 林西县| 雷波县| 太白县|