zoukankan      html  css  js  c++  java
  • Javascript+xmlhttp调用Webservice以及注意事项

     1 如何处理: 请求格式无法识别 这种调用过程的错误?
     2  -----------注意vs.net写wevservie供调用的时------
     3 <webServices> 
     4 <protocols> 
     5 <add name="HttpPost" /> 
     6 <add name="HttpGet" /> 
     7 </protocols> 
     8 </webServices> 
     9 把这个复制到你项目里的web.config里的<system.web>节点下面应该就可以了
    10 
    11 下面是具体调用过程:
    12 
    13 
    1.  创建webservice,为了免于落俗我稍稍修改了创建webserice的默认webmethod。^_^
    using System;
    using System.Web;
    using System.Web.Services;
    using System.Web.Services.Protocols;

    [WebService(Namespace 
    = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo 
    = WsiProfiles.BasicProfile1_1)]
    public class Service : System.Web.Services.WebService
    {
        public Service () 
    {

            
    //Uncomment the following line if using designed components 
            //InitializeComponent(); 
        }


        [WebMethod]
        public string SayHelloTo(string Name) 
    {
            
    return "Hello "+Name;
        }

        
    }

    还是俗了点。:)

    2. js调用webservice+xmlhttp的实现部分。

    <html>
    <title>
    Call webservice 
    with javascript and xmlhttp.
    </title>
    <body>
    <script language="javascript"> 

    //Test function with get method.
    function RequestByGet(data)

    var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
    //Webservice location.
    var URL="http://localhost:1323/WebSite6/Service.asmx/SayHelloTo?Name=Zach";
    xmlhttp.Open(
    "GET",URL, false); 
    xmlhttp.SetRequestHeader (
    "Content-Type","text/xml; charset=utf-8"); 
    xmlhttp.SetRequestHeader (
    "SOAPAction","http://tempuri.org/SayHelloTo"); 
    xmlhttp.Send(data); 
    var result = xmlhttp.status; 
    //OK
    if(result==200
    document.write(xmlhttp.responseText); 
    }
     
    xmlhttp 
    = null
    }
     

    //Test function with post method
    function RequestByPost(value)
    {
    var data;
    data 
    = '<?xml version="1.0" encoding="utf-8"?>'; 
    data 
    = data + '<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/">'; 
    data 
    = data + '<soap:Body>'; 
    data 
    = data + '<SayHelloTo xmlns="http://tempuri.org/">'; 
    data 
    = data + '<Name>'+value+'</Name>'; 
    data 
    = data + '</SayHelloTo>'; 
    data 
    = data + '</soap:Body>'; 
    data 
    = data + '</soap:Envelope>'; 

    var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
    var URL="http://localhost:1323/WebSite6/Service.asmx";
    xmlhttp.Open(
    "POST",URL, false); 
    xmlhttp.SetRequestHeader (
    "Content-Type","text/xml; charset=gb2312"); 
    xmlhttp.SetRequestHeader (
    "SOAPAction","http://tempuri.org/SayHelloTo"); 
    xmlhttp.Send(data); 
    document.write( xmlhttp.responseText); 

    }


    </Script>

    <input type="button" value="CallWebserviceByGet" onClick="RequestByGet(null)">
    <input type="button" value="CallWebserviceByPost" onClick="RequestByPost('Zach')">

    </body>
    </html>
    对于使用post方法需要发送的那堆东东可以在webservice的测试页面中找到,自己拼凑加上对应的参数就可以。

    我发现用post方法的时候响应很慢,是因为用Post方法时发送的数据多的原因吗?

  • 相关阅读:
    解决Sqlite Developer过期的最简单办法(转自百度经验)
    (转)解决!Visual Studio 遇到了异常。这可能是由某个扩展导致的。
    【转】VC中的class“std::vector<_Ty>”需要有 dll 接口由 class“Test”的客户端使用错误
    切换日语输入法找不到MicrosoftIME键盘选项了
    ps如何拆分图片
    码农应该注意保持的习惯
    "指定的文件格式无法识别或为不支持的二进制"
    编译FlashDemo遇到问题:Error "pFlashUI未定义的标识符"
    GridView,GridLayout
    Android,ArrayList,List,Set等
  • 原文地址:https://www.cnblogs.com/msn/p/888718.html
Copyright © 2011-2022 走看看