Ajax 跨域调用 webservice

1、web.config配置(在system.web 标签中加):

<webServices>
      <protocols>
        <add name="HttpPost" />
        <add name="HttpGet" />
      </protocols>
</webServices>

2、webservice端:

using System.Web;
using System.Web.Services;

namespace CacheService.Service
{
    /// <summary>
    /// CacheService 的摘要说明
    /// </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 CacheService : System.Web.Services.WebService
    {
        [WebMethod]
        public void Abc()
        {
            string callback = HttpContext.Current.Request[<span style="color:#FF0000;">"jsoncallback"</span>];
            bool bl = true;//这是我调用业务逻辑层(BLL)的一个方法
            //返回一个布尔(boolean)值
            //现在我省略掉,直接赋值true
            <span style="color:#FF0000;">HttpContext.Current.Response.Write(callback + "({result:'" + bl + "'})");</span>
            //关于result这词是你自己自定义的属性
            //会作为回调参数的属性供你调用结果
            HttpContext.Current.Response.End();
        }
    }
}

3、Jquery端( 跨域访问webservice,如:http://10.97.18.109:8001/Service/CacheService.asmx/Abc,要用jsonp和后缀加?jsoncallback=?):

$.ajax({
	type: "POST",
	contentType: "application/json;charset=utf-8",
	url: "http://10.97.18.109:8001/Service/CacheService.asmx/Abc<span style="color:#FF0000;">?jsoncallback=?</span>",
	dataType: "jsonp",
	jsonp: "jsonp",
	success: function (result) {
             alert(result);
             }
	});


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值