本文介绍了使用javascript消费休息服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 嗨... 我是javascript的新手。如何使用javascript调用restful web服务。 函数CreateXMLHttpRequest(){ if ( typeof XMLHttpRequest!= undefined){ // 所有现代浏览器(IE7 +,Firefox,Chrome,Safari和Opera)都使用XMLHttpRequest对象 return new XMLHttpRequest(); } 其他 如果( typeof ActiveXObject!= undefined){ // Internet Explorer(IE5和IE6)使用ActiveX对象 return new ActiveXObject( Microsoft.XMLHTTP ); } 其他 { throw new 错误( XMLHttpRequest不支持); } } 函数CallService(){ var objXMLHttpRequest = CreateXMLHttpRequest(); objXMLHttpRequest.onreadystatechange = function(){ if (objXMLHttpRequest.readyState == 4 ) objXMLHttpRequest.responseText; } objXMLHttpRequest.open( POST, http:// localhost:2546 / abc.svc / json / GetXml,真); objXMLHttpRequest.setRequestHeader( Content-Type, application / x-www-form-urlencoded); // objXMLHttpRequest.setRequestHeader(Content-Type,text / xml; charset = utf-8 ); var packet = ' <?xml version =1.0encoding =utf-8?>< CompanyRequest xmlns =http://schemas.datacontract.org/2004/07/abc.DomainModelxmlns:i = http://www.w3.org/2001/XMLSchema-instance ><公司>公司1< /公司>< / CompanyRequest>'; objXMLHttpRequest.send(包); } 我需要发送xml请求,响应也应该是xml。我作为回应得到null。如果我错了,请更正.. 提前感谢... 解决方案 这是如何使用ajax获取xml数据源的示例。 .ajax({ type: GET, url: sites1.xml, // urlOftheXml dataType: xml,成功: function (xml){ (xml).find(' func')。each( function (){ var number1 = Hi...i am very new to javascript. how to call restful web services using javascript. function CreateXMLHttpRequest() { if (typeof XMLHttpRequest != "undefined") { //All modern browsers (IE7+, Firefox, Chrome, Safari, and Opera) uses XMLHttpRequest object return new XMLHttpRequest(); } else if (typeof ActiveXObject != "undefined") { //Internet Explorer (IE5 and IE6) uses an ActiveX Object return new ActiveXObject("Microsoft.XMLHTTP"); } else { throw new Error("XMLHttpRequestnot supported"); } } function CallService() { var objXMLHttpRequest = CreateXMLHttpRequest(); objXMLHttpRequest.onreadystatechange = function () { if (objXMLHttpRequest.readyState == 4) objXMLHttpRequest.responseText; } objXMLHttpRequest.open("POST", "http://localhost:2546/abc.svc/json/GetXml", true); objXMLHttpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); // objXMLHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8"); var packet = '<?xml version="1.0"encoding="utf-8"?><CompanyRequest xmlns="http://schemas.datacontract.org/2004/07/abc.DomainModel" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><Company>company1</Company></CompanyRequest>'; objXMLHttpRequest.send(packet); }i need to send xml request and response should also be xml. i am getting null as response. correct if i am wrong..thanks in advance... 解决方案 This is sample how to use ajax to get xml data source..ajax({type : "GET",url : "sites1.xml",//urlOftheXml dataType : "xml",success : function(xml) {(xml).find('func').each(function() {var number1 = 这篇关于使用javascript消费休息服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-19 22:26