本人第一次在博客园写博客。

最近研究js的跨域调用,举个小例子。

ASP.net 中webservice 源代码

/// <summary>
    /// Service1 的摘要说明
    /// </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 Service1 : System.Web.Services.WebService
    {
        [WebMethod]
        public void GetProduct(string no)
        {
            List<Product> list = new List<Product>();
            
            DataSet ds = GetDs("MYDB", "select ProductCode,fCurQty from StorageTotal where StorageID=1 and CurQty>0 and ProductCode like '%" + no + "%' order by ProductCode");
           
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                list.Add(new Product() { Code = dr["ProductCode"].ToString(), Qty = Convert.ToInt32(dr["fCurQty"]) });
            }
           
            ResponseResult responseResult = new ResponseResult();
            if (list.Count == 0)
            {
                responseResult.ResponseDetails = "没有查到匹配的纪录!";
                responseResult.ResponseStatus = 0;
            }
            else
            {
                responseResult.ResponseData = list;
                responseResult.ResponseDetails = "查询库存信息成功!";
                responseResult.ResponseStatus = 1;
            }
            string jsoncallback = HttpContext.Current.Request["jsoncallback"];
            string result = jsoncallback + "(" + JsonConvert.SerializeObject(responseResult, Formatting.Indented) + ")";
            HttpContext.Current.Response.Write(result);
            HttpContext.Current.Response.End();
        }

  2.html 页面跨域调用方式

  

<script type="text/javascript">

var userData = {
            requestUrl: "http://oa.nextorch.com/WebService/Service1.asmx/GetProduct?jsoncallback=?",
            requestParams: { no: null },
            requestCallBack: function (json) {
                if (json.ResponseStatus == 1) {
                    userData.resultData = json.ResponseData;
                    var resultHtml = "";
                    $(userData.resultData).each(function () {
                        resultHtml += "编号" + this.Code;
                        resultHtml += " 数量" + this.Qty;
                        resultHtml += "<br/>";
                    });
                    $(".result").html(resultHtml);
                } else $(".result").html(json.ResponseDetails);
            },
            resultData: null
        };
        $(function () {
            $("#btnSearch").click(function () {
                userData.requestParams.no = $("#txtValue").val();
                $.getJSON(userData.requestUrl, userData.requestParams, userData.requestCallBack);
            });
        });

</script>

跨域调用webservice的更多相关文章

  1. jQuery跨域调用WebService

    jQuery跨域调用WebService举例html: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN&qu ...

  2. ajax跨域调用webservice例子

    [WebMethod(Description = "这是一个描述")] public void GetTIM() { try { SqlDataAdapter da = new S ...

  3. ajax使用jsonp跨域调用webservice error错误信息"readyState":4,"status":200,"statusText":"success"

    主要还是接口写有问题 至于ajax保持简洁写法即可 $.ajax({ dataType: 'jsonp', type: ‘get’, data: {}, url: '' })

  4. 浅谈跨域以及WebService对跨域的支持

    跨域问题来源于JavaScript的同源策略,即只有 协议+主机名+端口号 (如存在)相同,则允许相互访问.也就是说JavaScript只能访问和操作自己域下的资源,不能访问和操作其他域下的资源. 在 ...

  5. jquery Ajax跨域调用WebServices方法

    由于公司需要开发一个手机页面,想提供给同事直接在手机上可以查询SAP资料.数据需要使用js调用webserver来获取. 因为初次使用Jquery调用Webserver,所以期间并不顺利.测试调用We ...

  6. 浅谈跨域以WebService对跨域的支持

    跨域问题来源于JavaScript的同源策略,即只有 协议+主机名+端口号 (如存在)相同,则允许相互访问.也就是说JavaScript只能访问和操作自己域下的资源,不能访问和操作其他域下的资源. 在 ...

  7. Jquery的跨域调用

    JQuery1.2后getJSON方法支持跨域读取json数据,原理是利用一个叫做jsonp的概念.当然,究其本质还是通过script标签动态加载js,似乎这是实现真正跨域的唯一方法. getJSON ...

  8. 跨域以及WebService对跨域的支持

    无耻收藏该博主的成果啦!https://www.cnblogs.com/yangecnu/p/introduce-cross-domain.html 通过域验证访问WebService:https:/ ...

  9. AJAX跨域调用ASP.NET MVC或者WebAPI服务的解决方案

    问题描述 当跨域(cross domain)调用ASP.NET MVC或者ASP.NET Web API编写的服务时,会发生无法访问的情况. 重现方式 使用模板创建一个最简单的ASP.NET Web ...

随机推荐

  1. iOS中UISearchBar(搜索框)使用总结

    http://my.oschina.net/u/2340880/blog/509756

  2. .net 实现注册邮箱验证激活

    没事上网当了个注册邮箱验证激活的代码,用起来感觉还不错,特意和大家要一起分享一下 下面是主要实现代码: uing System.Net.Mail; public partial class jquer ...

  3. (转) How to install eclipse in ubuntu 12.04

    源地址:http://www.krizna.com/ubuntu/install-eclipse-in-ubuntu-12-04/ Eclipse installation in ubuntu 12. ...

  4. 制作 leanote docker 镜像

    leanote 使用 mongodb 存储数据,如果把 mongodb 单独做成一个镜像,初始化数据时比较麻烦,所以最后还是决定把 mongodb 和 leanote 放到同一个镜像里边. docke ...

  5. str_翻转字符串

    1. 给一个句子,翻转每个单词,单词内部不翻转 $str = "dog loves pig"; $ret = turnSentence($str); var_dump($ret); ...

  6. BZOJ 1061 志愿者招募

    http://www.lydsy.com/JudgeOnline/problem.php?id=1061 思路:可以用不等式的改装变成费用流. 将不等式列出,如果有负的常数,那么就从等式连向T,如果是 ...

  7. Keil C51编译及连接技术

    主要介绍Keil C51的预处理方法如宏定义.常用的预处理指令及文件包含指令,C51编译库的选择及代码优化原理,C51与汇编混合编程的方法与实现以及超过64KB空间的地址分页方法的C51实现. 教学目 ...

  8. Qt判断和打开进程(windows端),运行,检测,中止

    windows端的Qt程序往往需要打开外部程序(如:prog.exe),并且需要确定这个外部程序是唯一打开的. 1.判断系统中是否存在prog.exe void judge() { QProcess ...

  9. windows下将隐藏文件通过命令行改变为普通的显示文件

    attrib -h -s *.* /s /d 不懂的看下 attrib /? 帮助.保存为bat也可以.

  10. 单路CPU性能排名 更新于2015.10.6

    http://itianti.sinaapp.com/index.php/cpu 排名 处理器 图例 分数 1 Intel Xeon E5-2699 v3 @ 2.30GHz 22892 2 Inte ...