zoukankan      html  css  js  c++  java
  • 在JavaScript中调用WebService

    JavaScript中调用WebService

    左直拳

    试着在JavaScript中调用了一下WebService。觉得有两个地方要注意。

    1、参数传递。

    一般调用WebService的方法,都要传参数。怎么传?如下:

    WebService部分】

    文件:UserManage/UserInfo.asmx

    方法:

    [WebMethod]

    public string GetUserName(string accounts)

     

    JavaScript部分】

     

    //WebService地址及方法名称。其中GetUserName是要调用的方法

    var URL = "UserManage/UserInfo.asmx/GetUserName";

    var Params = "accounts=leftfist";//传给WebService的参数

    var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

    xmlhttp.Open("POST",URL, false);//POST方法

    xmlhttp.SetRequestHeader("Content-Type","application/x-www-form-urlencoded");

    xmlhttp.SetRequestHeader ("Content-Length",Params.length);

    xmlhttp.send(Params);

    if( xmlhttp.Status == 200 ){//200代表成功

    var res = xmlhttp.responseXML;//得到WebService传回的结果

    return res.childNodes[1].text + "";

    }

    return xmlhttp.responseText;

     

    2、配置文件(Web.config)问题。

    JavaScript脚本写好以后,在本地上运行并无问题。但上传到服务器却显示:

    Request format is unrecognized for URL unexpectedly ending in '/GetUserName'

    估计是"UserManage/UserInfo.asmx/GetUserName"这种调用WebService的写法服务器还不认,找了很久才知道,应该在Web.config里加上:

        <webServices>

          <protocols>

            <add name="HttpGet"/>

            <add name="HttpPost"/>

          </protocols>

    </webServices>

     
  • 相关阅读:
    【centos6.5 安装 node.js + npm】
    【钉钉PC】PC端钉钉清除缓存
    【laravel5.4】中jquery的post Ajax提交
    python 设计模式之中介者模式
    python 设计模式之备忘录模式
    python 设计模式之观察者模式
    python 设计模式之策略模式
    23种设计模式有哪些,不带定义,不带例子
    python 设计模式之模板方法模式
    python 设计模式之访问者模式
  • 原文地址:https://www.cnblogs.com/leftfist/p/4258316.html
Copyright © 2011-2022 走看看