chrome和Firefox中的响应为空 [英] response is empty in chrome and firefox

查看:73
本文介绍了chrome和Firefox中的响应为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨...

我想使用javascript调用休息服务.我的代码是

Hi...

i want to call rest services using javascript. my code is

function CreateXMLHttpRequest() {


             if (typeof XMLHttpRequest != "undefined") {
                 alert("1");
                 //All modern browsers (IE7+, Firefox, Chrome, Safari, and Opera) uses XMLHttpRequest object
                 return new XMLHttpRequest();
             }
             else if (typeof ActiveXObject != "undefined") {
                 alert("2");
                 //Internet Explorer (IE5 and IE6) uses an ActiveX Object
                 return new ActiveXObject("Microsoft.XMLHTTP");
             }
             else {
                 throw new Error("XMLHttpRequestnot supported");
             }
         }
         function CallWebService() {
             var objXMLHttpRequest = null;
                    objXMLHttpRequest = CreateXMLHttpRequest();

             objXMLHttpRequest.open("POST", "http://localhost:2546/abc.svc/json/GetXml", true);
             objXMLHttpRequest.setRequestHeader("Content-Type", "application/xml;charset=UTF-16");
             var packet = '<GetCompanyRequest xmlns="http://schemas.datacontract.org/2004/07/abc.DomainModel" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><CompanyName>company</CompanyName></GetCompanyRequest>';
//          
             objXMLHttpRequest.send(packet);
            
            alert(objXMLHttpRequest.responseText);                  

         }



它在IE中的工作正常,状态为200.但是在chrome和firefox中,状态为0,响应为空.还有我该如何解析响应.....
让我知道是否有任何错误.
在此先感谢....



Its working fine in IE and status is 200. But in chrome and firefox status is 0 and response is empty. And also how can i parse the response.....
let me know if any errors.
thanks in advance....

推荐答案

尝试用这种方法检查浏览器的兼容性,
Try this way to check the Browser Compability,
if(window.XMLHttpRequest)
            {
                // Other Browser
                ajaxRequest = new XMLHttpRequest();
            }
            else
            {
                // Internet Explorer
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            }
            ajaxRequest.onreadystatechange=function()
                {
                    if(ajaxRequest.readyState == 4)
                        {
                            document.getElementById("YourOutPutID").innerHTML = ajaxRequest.responseText;
                        }
                }
                ajaxRequest.open("GET", "index.php?vote="+vote,true);
                ajaxRequest.send();

          }


这篇关于chrome和Firefox中的响应为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆