首页 > Web开发 > 详细

ajax调用.net webservice

时间:2015-05-29 18:14:06      阅读:272      评论:0      收藏:0      [点我收藏+]

JS调用.net  webservice存在跨域问题。调用方式如下:

JS前台:



            var url = "http://localhost:4263/zbhjjcWeb/Service.asmx/HelloWord";
            var str = "v1=‘123‘&v2=‘Boston‘&jsonpCallback=HelloWord"; //参数传递,其中v1和v2为.net webservice中函数参数,jsonpCallback为.net webservice中的函数
            jQuery.ajax(url, {
                type: "GET",   //你选择get或者post最后都是get,跨域情况下都是get
                contentType: "application/json",
                data: str,         //这里是要传递的参数
                dataType: "jsonp",
                jsonp: ‘callback‘,                          //服务器端的回调函数名
            //    jsonpCallback: ‘HelloWord‘,                   //回调函数名


                success: function (result) {     //回调函数,result,返回值
                    alert(result.value1 + ";" + result.value2);
                },
                error: function (err) { alert(err.textStatus); }
            });


webservice代码:

     [WebMethod]
    public void HelloWord(String v1,String v2)
    {
        string callback = Context.Request["callback"];  
        string response = "{\"value1\":\"" + v1 + "\",\"value2\":\"" + v2 + "\"}";
        string call = callback + "(" + response + ")";
        Context.Response.Write(call);
        Context.Response.End();  
    }

ajax调用.net webservice

原文:http://blog.csdn.net/shileimohan/article/details/46237095

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!