博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JavaScript+Html 调用Wcf Rest Api接口
阅读量:6161 次
发布时间:2019-06-21

本文共 4105 字,大约阅读时间需要 13 分钟。

  1. 功能需求
         公司使用wcf rest搭建了一个服务,为了使不同网站客户能够访问这个服务,决定用最通用的javascript+html 嵌入到客户网站调用该服务。要求使用简单,数据格式为xml,并能兼容大部分浏览器(IE6-IE9、360、firefox、chrome)等
  2. 解决思路
          使用javascript+ajax请求的方式,调用远程服务。遇到的主要问题是如何解决浏览器兼容问题,比如IE8+调用远程服务的时候会遇到跨域问题,程序中是通过创建ActiveXObject解决的,而不是标准的XMLHttpRequest
  3. 主要代码解释
    1. IE浏览器,解决跨域访问问题
              
         
      function crossDomainIE() {
                     
      // Use Microsoft XDR
                     
      var xdr
      =
      new XDomainRequest();
                     xdr.open(
      "POST", url);
                     xdr.onload
      =
      function () {
                         
      // XDomainRequest doesn't provide responseXml, so if you need it:
                         
      var dom
      =
      new ActiveXObject(
      "Microsoft.XMLDOM");
                         dom.async
      = false;
                         displayData(xdr.responseText);
                     };
                     xdr.send(data);
                 }
    2. 其它浏览器,默认无跨域问题
      function noCrossDomain() {
                     
      var xmlhttp
      = createXMLHttp();
                     xmlhttp.open(
      "POST", url);
                     xmlhttp.setRequestHeader(
      "Content-Type",
      "application/x-www-form-urlencoded");
                     
      //xmlhttp.setRequestHeader("Access-Control-Allow-Origin", "*");
                     xmlhttp.send(data);
                     
      // Create the callback:
                     xmlhttp.onreadystatechange
      =
      function () {
                         
      if (xmlhttp.readyState
      !=
      4) {
                             
      return;
      // Not there yet
                         }
                         
      if (xmlhttp.status
      !=
      200) {
                             
      return;
                         }
                         displayData(xmlhttp.responseText);
                     }
                 }
    3. XML文本转换为Xml Dom也存在浏览器兼容问题
           
      function convertXml2Dom(xmlData) {
                     
      if (window.ActiveXObject) {
                         
      //for IE
                         xmlDoc
      =
      new ActiveXObject(
      "Microsoft.XMLDOM");
                         xmlDoc.async
      =
      "false";
                         xmlDoc.loadXML(xmlData);
                         
      return xmlDoc;
                     }
      else
      if (document.implementation
      && document.implementation.createDocument) {
                         
      //for Others
                         parser
      =
      new DOMParser();
                         xmlDoc
      = parser.parseFromString(xmlData,
      "text/xml");
                         
      return xmlDoc;
                     }
                 }
    4. 解决firefox无法用innerText显示文本的问题1
      //显示查询结果
              
      function displayData(resp) {
                  
      var xmlObject
      = convertXml2Dom(resp);
                  document.getElementById(
      "lblAge").innerText
      = xmlObject.getElementsByTagName(
      "Age")[
      0].childNodes[
      0].nodeValue;
                  
      //相同值再次赋值给innerHTML是应为innerText在firefox中无效,暂不知道原因
                  document.getElementById(
      "lblAge").innerHTML
      = xmlObject.getElementsByTagName(
      "Age")[
      0].childNodes[
      0].nodeValue;
              }
  4. 完整源代码
    <script type
    =
    "text/javascript"
    >
            
    var url
    =
    "远程服务url地址";
            
    var data;
            window.onload
    =
    function () {
                
    //请求xml数据
                data
    =
    "\
                      <?xml version='1.0' encoding='utf-8'?>\
                      <xmlhttpuest>\
                        <FirstName>Chen</FirstName>\
                        <LastName>Fox</LastName>\
                       </xmlhttpuest>\
                     "
    ;
                
    if (window.XDomainRequest) {
    //为了解决IE的跨域问题
                    crossDomainIE();
                }
                
    else {
                    noCrossDomain();
                }
            }
            
    //IE浏览器,解决跨域访问
            
    function crossDomainIE() {
                
    // Use Microsoft XDR
                
    var xdr
    =
    new XDomainRequest();
                xdr.open(
    "POST", url);
                xdr.onload
    =
    function () {
                    
    // XDomainRequest doesn't provide responseXml, so if you need it:
                    
    var dom
    =
    new ActiveXObject(
    "Microsoft.XMLDOM");
                    dom.async
    = false;
                    displayData(xdr.responseText);
                };
                xdr.send(data);
            }
            
    //其它浏览器,默认无跨域问题
            
    function noCrossDomain() {
                
    var xmlhttp
    = createXMLHttp();
                xmlhttp.open(
    "POST", url);
                xmlhttp.setRequestHeader(
    "Content-Type",
    "application/x-www-form-urlencoded");
                
    //xmlhttp.setRequestHeader("Access-Control-Allow-Origin", "*");
                xmlhttp.send(data);
                
    // Create the callback:
                xmlhttp.onreadystatechange
    =
    function () {
                    
    if (xmlhttp.readyState
    !=
    4) {
                        
    return;
    // Not there yet
                    }
                    
    if (xmlhttp.status
    !=
    200) {
                        
    return;
                    }
                    displayData(xmlhttp.responseText);
                }
            }
            
    //创建ajax http对象
            
    function createXMLHttp() {
                
    var xmlhttp;
                
    if (window.XMLHttpRequest) {
    // code for IE7+, Firefox, Chrome, Opera, Safari
                    xmlhttp
    =
    new XMLHttpRequest();
                }
                
    else {
    // code for IE6, IE5
                    xmlhttp
    =
    new ActiveXObject(
    "Microsoft.XMLHTTP");
                }
                
    return xmlhttp;
            }
            
    //将xml文本转换为xml dom
            
    function convertXml2Dom(xmlData) {
                
    if (window.ActiveXObject) {
                    
    //for IE
                    xmlDoc
    =
    new ActiveXObject(
    "Microsoft.XMLDOM");
                    xmlDoc.async
    =
    "false";
                    xmlDoc.loadXML(xmlData);
                    
    return xmlDoc;
                }
    else
    if (document.implementation
    && document.implementation.createDocument) {
                    
    //for Others
                    parser
    =
    new DOMParser();
                    xmlDoc
    = parser.parseFromString(xmlData,
    "text/xml");
                    
    return xmlDoc;
                }
            }
            
    //显示查询结果
            
    function displayData(resp) {
                
    var xmlObject
    = convertXml2Dom(resp);
                document.getElementById(
    "lblAge").innerText
    = xmlObject.getElementsByTagName(
    "Age")[
    0].childNodes[
    0].nodeValue;
                
    //相同值再次赋值给innerHTML是应为innerText在firefox中无效,暂不知道原因
                document.getElementById(
    "lblAge").innerHTML
    = xmlObject.getElementsByTagName(
    "Age")[
    0].childNodes[
    0].nodeValue;
            }
        
    <
    /script
    >

转载于:https://www.cnblogs.com/gossip/archive/2012/09/14/2684948.html

你可能感兴趣的文章
爬虫豆瓣top250项目-开发文档
查看>>
有趣的数学书籍
查看>>
teamviewer 卸载干净
查看>>
eclipse的maven、Scala环境搭建
查看>>
架构师之路(一)- 什么是软件架构
查看>>
USACO 土地购买
查看>>
【原创】远景能源面试--一面
查看>>
B1010.一元多项式求导(25)
查看>>
10、程序员和编译器之间的关系
查看>>
配置 RAILS FOR JRUBY1.7.4
查看>>
修改GRUB2背景图片
查看>>
Ajax异步
查看>>
好记性不如烂笔杆-android学习笔记<十六> switcher和gallery
查看>>
JAVA GC
查看>>
3springboot:springboot配置文件(外部配置加载顺序、自动配置原理,@Conditional)
查看>>
图解SSH原理及两种登录方法
查看>>
查询个人站点的文章、分类和标签查询
查看>>
基础知识:数字、字符串、列表 的类型及内置方法
查看>>
JSP的隐式对象
查看>>
JS图片跟着鼠标跑效果
查看>>