In my case, I chose to convert XML directly to JSON for subsequent processing of JavaScript applications. I use the. NET platform to build simple webservice.
Request.asmx
Copy Code code as follows:
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>
Summary description of Service1
</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)});
}
}
The above is just a simple test use to facilitate subsequent use of JavaScript to handle different types of data
For JavaScript, it's definitely using the XMLHttpRequest object to access the server side, but for the sake of simplicity, I didn't consider compatibility issues, directly using the XMLHttpRequest object (I used Chrome as a test browser), For this I use the Ajaxclient class for the HTTP operation (Post method), WebService class to encapsulate the processing webservice (invoke the Ajaxclient class as the action Class), Jsonconverter class processes XML data into JSON data
Common.js (including Jsonconverter class)
Copy Code code as follows:
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
Copy Code code as follows:
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
Copy code code as follows:
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;
}
}
The call is simple, just
Copy Code code as follows:
var webservice=new webService (' http://localhost/NightKidsWebService/Request.asmx ');
Webservice.request ("getresourcelist", null);
Alert (Jsonconverter.convertfromxml (Webservice.getresponse (). firstchild));
The first parameter in the request method corresponds to a different service name, and the second parameter is added to the corresponding service Parameter table (JSON format, for example: {id:123,name: ' Nightkids '})