Ajax在.Net 2.0中启用了Web服务 [英] Ajax Enabled Webservice in .Net 2.0
问题描述
如何在.Net 2.0中创建支持Ajax的Web服务
How to create Ajax Enabled Webservice in .Net 2.0
function Likes_Count() {
debugger;
// alert(row);
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "http://localhost:1157/WebSite2/likes/likesservice.asmx/LikesCount",
//data: '{"Cat":' + cat + ',"title":' + title + '}',
data: "{ 'Cat': '" + cat + "','title': '" + title + "'}",
dataType: "json",
success: function (data) {
$("#likes_count").html(data.d);
},
error: function (result) {
alert("Error");
}
});
}
window.onload = Likes_Count;
网络服务代码
webservice code
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
/// <summary>
/// Summary description for LikesService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
[ScriptService]
public class LikesService : System.Web.Services.WebService {
public LikesService () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string AddLikes(string cat, string title)
{
return cat;
}
[WebMethod]
public string[] Likes_Count()
{
string[] arr4 = new string[1];
arr4[0] = "Success";
return arr4;
}
[WebMethod]
public string LikesCount(string Cat, string title)
{
return "98948";
}
}
< b。在.net 2.0中找不到System.Web.Script.Services 名称空间。但是在3.5和4.0中找到了
这个代码在3.5和4.0中运行良好
所以请告诉替代方法创建一个启用ajax的webservice .net 2.0
System.Web.Script.Services namespace is not found in .net 2.0. but found in 3.5 and 4.0
this code works well in 3.5 and 4.0
so please tell the alternate way to create ajax enabled webservice in .net 2.0
推荐答案
.ajax({
type: POST,
contentType: application / json; charset = utf -8,
url: http:// localhost:1157 / WebSite2 / likes /likesservice.asmx/LikesCount,
// data:'{Cat: '+ cat +',title:'+ title +'}',
data: {'Cat':' + cat + ','title':' + title + '},
dataType: json ,
成功: function (data){
.ajax({ type: "POST", contentType: "application/json; charset=utf-8", url: "http://localhost:1157/WebSite2/likes/likesservice.asmx/LikesCount", //data: '{"Cat":' + cat + ',"title":' + title + '}', data: "{ 'Cat': '" + cat + "','title': '" + title + "'}", dataType: "json", success: function (data) {
( #likes_count)。html(data.d);
},
错误: function (结果){
alert( 错误);
}
});
}
window .onload = Likes_Count;
("#likes_count").html(data.d); }, error: function (result) { alert("Error"); } }); } window.onload = Likes_Count;
网络服务代码
webservice code
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
/// <summary>
/// Summary description for LikesService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
[ScriptService]
public class LikesService : System.Web.Services.WebService {
public LikesService () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string AddLikes(string cat, string title)
{
return cat;
}
[WebMethod]
public string[] Likes_Count()
{
string[] arr4 = new string[1];
arr4[0] = "Success";
return arr4;
}
[WebMethod]
public string LikesCount(string Cat, string title)
{
return "98948";
}
}
< b。在.net 2.0中找不到System.Web.Script.Services 名称空间。但是在3.5和4.0中找到了
这个代码在3.5和4.0中运行良好
所以请告诉替代方法创建一个启用ajax的webservice .net 2.0
System.Web.Script.Services namespace is not found in .net 2.0. but found in 3.5 and 4.0
this code works well in 3.5 and 4.0
so please tell the alternate way to create ajax enabled webservice in .net 2.0
这篇关于Ajax在.Net 2.0中启用了Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!