当前位置: 代码迷 >> ASP.NET >> JQuery跨域调用webservice有关问题
  详细解决方案

JQuery跨域调用webservice有关问题

热度:4911   发布时间:2013-02-25 00:00:00.0
JQuery跨域调用webservice问题
JScript code
页面JS代码如下:方法一:$.getJSON(    "http://ip地址/chatService/chatService.asmx/userLogin?jsoncallback=?",    {userId:"'"+$("#userId").val()+"'", serverId:"'"+$("#serverId").val()+"'", date:"'"+new Date()+"'"},    function(json)    {       if(json.length > 0)       {           alert(json);                            }       }); 方法二:               $.ajax({                    async: false,                    type: "POST",                    contentType:"application/json;utf-8",                    url: "http://ip地址/chatService/chatService.asmx/userLogin?callback=?",                    data: "{userId:'"+$("#userId").val()+"', serverId:'"+$("#serverId").val()+"', date:'"+ new Date() +"'}",                    datatype:"json",                    success: function(json){                        if(json != "")                        {                            alert(json);                        }                    }                });

C# code
web service方法已经包含了[System.Web.Script.Services.ScriptService]        [WebMethod]        public void userLogin(string userId, string serverId, string date)        {            DBManager db = new DBManager("COM");            SqlParameter[] p = new SqlParameter[4];            p[0] = new SqlParameter("@userId", SqlDbType.Int, 0);            p[0].Direction = ParameterDirection.Input;            p[0].Value = userId;            p[1] = new SqlParameter("@serverId", SqlDbType.Int, 0);            p[1].Direction = ParameterDirection.Input;            p[1].Value = serverId;            p[2] = new SqlParameter("@date", SqlDbType.DateTime, 0);            p[2].Direction = ParameterDirection.Input;            p[2].Value = date;            p[3] = new SqlParameter("@returns", SqlDbType.Int, 0);            p[3].Direction = ParameterDirection.ReturnValue;            int sysId = db.ExeProc("ap_webUserLog", p);            string callback = HttpContext.Current.Request["jsoncallback"];            string retV = "";            retV += "({state:" + sysId + ", msg:'成功建立连接'})";            HttpResponse response = HttpContext.Current.Response;            response.Write(callback + retV);            response.End();        }


现在的问题是:
用方法一,则整个getJSON执行不到内部
用方法二,jquery库文件中j.open(G,M.URL, async)提示没有权限。
跨域问题网上也看了,搞了下还是没明白。故求教。

------解决方案--------------------------------------------------------
你看下是否 那个webservice 不支持 http get 请求
------解决方案--------------------------------------------------------
帮你看了2方法

$.ajax({
dataType: "jsonp",
jsonp: 'jsoncallback',
url: "http://ip地址/chatService/chatService.asmx/userLogin?callback=?",
data: '_Method=GET&_JSON=' + "{userId:'"+$("#userId").val()+"', serverId:'"+$("#serverId").val()+"', date:'"+ new Date() +"'}",

success: function(json){
if(json != "")
{
alert(json);
}
}
});

------解决方案--------------------------------------------------------
看webservie所在项目的配置文件中 是否有
<add name="HttpGet"/>
  相关解决方案