fzu2013 2013-08-13 02:03 采纳率: 0%
浏览 1676

使用xmlhttp访问webservice时,得到的xmlhttp.status=0.而且得不到返回的xml文件

服务器提供了
POST /LoginVerifyWebService/Login.asmx/Login HTTP/1.1
Host: 10.10.10.191
Content-Type: application/x-www-form-urlencoded
Content-Length: length

username=string&password=string&randomString=string

JS代码:
var xmlhttp;
function createXMLHttpRequest(){

    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
         xmlhttp=new XMLHttpRequest();
    }
    else
    if(window.ActiveXObject)
    {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    else{
        alert("xmlhttp出错");
    }
 }

  function btn_onclick(){
    createXMLHttpRequest();     
    var url="http://10.10.10.191/LoginVerifyWebService/Login.asmx/Login?username=wuhl&password=Wf34566543&randomString=234234";

    xmlhttp.open("GET", url, true);
    xmlhttp.onreadystatechange=Response;        
    xmlhttp.send(null);     
}       
    function Response(){
        alert("a"+xmlhttp.readyState);
        if(xmlhttp.readyState==4){
            alert("xmlhttp.status="+xmlhttp.status);
            if(xmlhttp.status==200){
                alert("c"+xmlhttp.readyState);
                var domObj=xmlhttp.responsetext;
                var messageNodes=domObj.getElementsByTagName("Result");
                alert(messageNodes.firstChild.nodeValue);
            }else{
                alert("请求出错");
            }
        }
    }
</script>
  • 写回答

1条回答

  • ChatGPTᴼᴾᴱᴺᴬᴵ 2023-01-12 12:55
    关注

    您的代码在使用XMLHttpRequest对象发起一个GET请求来访问Web服务,并在返回的响应中查找元素。


    从服务器端的提供的接口中看,服务器需要的请求方式是 POST,并且需要传入Content-Type: application/x-www-form-urlencoded格式的参数。


    但是您的代码中使用的却是GET请求和没有传入Content-Type参数。


    为了解决这个问题,您需要更改代码中的请求方式为POST,并且设置Content-Type和Content-Length。

    xmlhttp.open("POST", url, true);
    xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    var data = "username=wuhl&password=Wf34566543&randomString=234234";
    xmlhttp.setRequestHeader("Content-Length", data.length);
    xmlhttp.onreadystatechange=response;
    xmlhttp.send(data);
    

    这样修改之后会符合服务器的要求,访问结果应该就可以正常了。

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。