标签:style blog http color 使用 os io strong
假设有一个基于.Net的Web Service,其名称为SaveProduct
POST /ProductService.asmx HTTP/1.1 Host: localhost Content-Type: text/xml; charset=utf-8 Content-Length: length SOAPAction: "http://sh.inobido.com/SaveProduct" <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <SaveProduct xmlns="http://sh.inobido.com/"> <productID>int</productID> <productName>string</productName> <manufactureDate>dateTime</manufactureDate> </SaveProduct> </soap:Body> </soap:Envelope>
在客户端用jQuery的ajax调用的代码为
var productServiceUrl = ‘http://localhost:57299/ProductService.asmx?op=SaveProduct‘; // Preferably write this out from server side function beginSaveProduct(productID, productName, manufactureDate){ var soapMessage = ‘<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <SaveProduct xmlns="http://sh.inobido.com/"> <productID>‘ + productID + ‘</productID> <productName>‘ + productName + ‘</productName> <manufactureDate>‘ + manufactureDate + ‘</manufactureDate> </SaveProduct> </soap:Body> </soap:Envelope>‘; $.ajax({ url: productServiceUrl, type: "POST", dataType: "xml", data: soapMessage, complete: endSaveProduct, contentType: "text/xml; charset=\"utf-8\"" }); return false; } function endSaveProduct(xmlHttpRequest, status){ $(xmlHttpRequest.responseXML) .find(‘SaveProductResult‘) .each(function() { var name = $(this).find(‘Name‘).text(); }); }
说明:
如用jQuery处理传回的XML响应,必须理解SOAP reponse’s schema定义,SaveProduct的schema格式如下:
HTTP/1.1 200 OK Content-Type: text/xml; charset=utf-8 Content-Length: length <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <SaveProductResponse xmlns="http://sh.inobido.com/"> <SaveProductResult> <ID>int</ID> <Name>string</Name> <ManufactureDate>dateTime</ManufactureDate> </SaveProductResult> </SaveProductResponse> </soap:Body> </soap:Envelope>
传回的响应放在xmlHttpRequest的参数responseXML中,可以使用firebug或Dev. Tool查看,现在我们就可以使用jQuery来遍历该XML的节点并处理数据了。
摘录自:http://openlandscape.net/2009/09/25/call-soap-xm-web-services-with-jquery-ajax/
使用JQuery的Ajax调用SOAP-XML Web Services(Call SOAP-XML Web Services With jQuery Ajax)(译+摘录)
标签:style blog http color 使用 os io strong
原文地址:http://www.cnblogs.com/JoannaQ/p/3900489.html