您的位置:首页 > 其它

Ajax 跨域调用 webservice

2014-12-26 15:03 260 查看
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);
}
});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: