关于ajax调用.NET的WebService的问题

刘小安 2014-08-13 02:21:18
WebService是我们自己的项目,ajax是别人调用,但是我需要给别人写个例子。
WebService是带参数的,我现在用ajax调用后总是error报错,报错内容是空的。我贴下代码

这是WebService的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using System.Web.Script.Serialization;

namespace MeierbeiWebDtService
{
/// <summary>
/// WebService 的摘要说明
/// </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 WebService : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public string HelloWorld()
{
JavaScriptSerializer js = new JavaScriptSerializer();
Context.Response.Clear();
Context.Response.ContentType = "application/json";
Models.HelloWorldData data = new Models.HelloWorldData();
data.Message = "HelloWorld";
data.Name = "MicJackson";
return js.Serialize(data);
}

[WebMethod]
[ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
public void GetClass(int fenshu)
{
JavaScriptSerializer js = new JavaScriptSerializer();
Context.Response.Clear();
Context.Response.ContentType = "application/json";
Models.HelloWorldData data = new Models.HelloWorldData();
if (fenshu > 90)
{
data.Message = "优异";
data.Name = "语文";
}
else if (fenshu > 80)
{
data.Message = "良好";
data.Name = "语文";
}
else if (fenshu > 60)
{
data.Message = "及格";
data.Name = "语文";
}
else
{
data.Message = "不及格";
data.Name = "语文";
}
Context.Response.Write(js.Serialize(data));
Context.Response.End();
//return js.Serialize(data);
}
}


}



这是我写的html代码
<html>
<head>
<script src="jquery-1.11.1.min.js" type="text/javascript"></script>
</head>
<body>
<script type="text/javascript">
$(document).ready(function(){
$.ajax({
url: "http://localhost:53568/WebService.asmx/GetClass",
type: "GET",
dataType: "json",
data: {fenshu: 23},
success: function(json) {
alert("success:"+json);
},
error: function(x, e) {
alert("error:"+x.responseText);
},
complete: function(x) {
alert("complete:"+x.responseText);
}
});

$.ajax({
url: "http://localhost:53568/WebService.asmx/HelloWorld",
type: "GET",
dataType: "json",
success: function(json) {
alert("success:"+json);
},
error: function(x, e) {
alert("error:"+x.responseText);
},
complete: function(x) {
alert("complete:"+x.responseText);
}
});
});
</script>
</body>
</html>


求助,哪里有问题,为什么得不到WebService返回的json数据啊?还有就是如果我想传参,有什么好点的办法吗?谢谢
...全文
443 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
May_BeTrue 2015-09-21
  • 打赏
  • 举报
回复
太爽了 大赞
天下如山 2014-08-13
  • 打赏
  • 举报
回复
果然是跨域...
天下如山 2014-08-13
  • 打赏
  • 举报
回复
跨域调用??
刘小安 2014-08-13
  • 打赏
  • 举报
回复
额,自己解决了,是因为我的html页面不在同一个域里,没跨域,现在加上jsonp就没问题了
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using System.Web.Script.Serialization;

namespace MeierbeiWebDtService
{
    /// <summary>
    /// WebService 的摘要说明
    /// </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 WebService : System.Web.Services.WebService
    {
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public string HelloWorld()
        {
            JavaScriptSerializer js = new JavaScriptSerializer();
            Context.Response.Clear();
            Context.Response.ContentType = "application/json";
            Models.HelloWorldData data = new Models.HelloWorldData();
            data.Message = "HelloWorld";
            data.Name = "MicJackson";
            return js.Serialize(data);
        }

        [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public void GetClass(int fenshu)
        {
            JavaScriptSerializer js = new JavaScriptSerializer();
            Context.Response.Clear();
            Context.Response.ContentType = "application/json";
            Models.HelloWorldData data = new Models.HelloWorldData();
            if (fenshu > 90)
            {
                data.Message = "优异";
                data.Name = "语文";
            }
            else if (fenshu > 80)
            {
                data.Message = "良好";
                data.Name = "语文";
            }
            else if (fenshu > 60)
            {
                data.Message = "及格";
                data.Name = "语文";
            }
            else
            {
                data.Message = "不及格";
                data.Name = "语文";
            }
            HttpRequest Request = HttpContext.Current.Request;
            string callback = Request["jsonp"];
            Context.Response.Write(callback + "(" + js.Serialize(data) + ")");
            Context.Response.End();
            //return js.Serialize(data);
        }
    }


}
<html>
<head>
	<script src="jquery-1.11.1.min.js" type="text/javascript"></script>
</head>
<body>
	<script type="text/javascript">
		function callbackjsp(result){
			alert("Message:"+result.Message + ",Name:"+result.Name);
		}
		$(document).ready(function(){
			$.ajax({
                url: "http://localhost:53568/WebService.asmx/GetClass",
                type: "GET",
                dataType: "jsonp",
				jsonp: "jsonp", //传递给请求处理程序或页面的,用以获得jsonp回调函数名的参数名(默认为:callback)
                jsonpCallback: "callbackjsp", //自定义的jsonp回调函数名称,默认为jQuery自动生成的随机函数名
                data: { fenshu: 93 },
                success: function (json) {
                    alert("success:" + json.Message);
                },
                error: function (x, e) {
                    //alert("error:" + x.responseText);
                },
                complete: function (x) {
                    //alert("complete:" + x.responseText);
                }
            });		
		});
	</script>
</body>
</html>
刘小安 2014-08-13
  • 打赏
  • 举报
回复
引用 1 楼 sibiyellow 的回复:
自己写的 “UseHttpGet = false” 而你的js又用GET方式去访问.
GET和POST都没用,我都试过的。
天下如山 2014-08-13
  • 打赏
  • 举报
回复
自己写的 “UseHttpGet = false” 而你的js又用GET方式去访问.

62,241

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧