zoukankan      html  css  js  c++  java
  • JS调用webservice服务

    webservice服务

    webservice服务代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Services;
    
    namespace Harmonysoft.Web
    {
        /// <summary>
        /// WebService1 的摘要说明
        /// </summary>
        [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        [System.ComponentModel.ToolboxItem(false)]
        // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。 
        [System.Web.Script.Services.ScriptService]
        public class WebService1 : System.Web.Services.WebService
        {
    
            [WebMethod]
            public string HelloWorld1(string name)
            {
                return "{data:你好:" + name + "}";
            } 
        }
    }
    View Code

    在服务中调用引用的服务,并将Ajax脚本调用改取消的注释取消

    JS调用

    JS调用方式同一站点的webservice

     var name=$("#birthday").val();
     $.ajax({
           type: "Post", //Post传参
           url: "/WebService1.asmx/HelloWorld1",//服务地址
           data: "{name:'" + name + "'}",//参数
           dataType: "json",
           contentType: "application/json;charset=utf-8",
           success: function (result) {
                        // 调用成功后,将获取的名字填入name文本框中。
                        $("#name").val(result.d);
           },
           error: function (e) {
              window.alert(e.status);
           }
      })

    调用结果

    跨域访问

    见另一篇文章 JS跨域访问

  • 相关阅读:
    android-基础编程-RecyclerView
    android-基础编程-ListView
    LINUX 日志服务器的搭建
    使用parted进行磁盘分区
    raid磁盘阵列
    LVM逻辑卷管理
    /home 分区迁移试验
    PHP 匹配一个汉字
    xhr dojo load
    ERR: Call to undefined function openssl_random_pseudo_bytes()
  • 原文地址:https://www.cnblogs.com/yx007/p/8073238.html
Copyright © 2011-2022 走看看