Ajax and ASP.Net Web Service
来源:互联网 发布:淘宝企业开店怎么注册 编辑:程序博客网 时间:2024/06/08 07:53
Web Service (ASMX, ASPX, WCF ) and Ajax Call
1. Use JQuery Ajax call ASMX web method directly
The reason need to add [System.Web.Script.Services.ScriptService] is Ajax does not support SOAP message, which provide byasmx by default.
[System.Web.Script.Services.ScriptService]
public classWebService1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
}
function CallService() {
$.ajax({
url: "WebService1.asmx/HelloWorld",
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: "{}",
success: function (data, textStatus) {
if (textStatus == "success") {
alert(data.d);
}
},
error: function (data, status, error) {
alert(error);
}
});
}
OR
[System.Web.Script.Services.ScriptService]
public classWebService1 : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(UseHttpGet =true)]
public string HelloWorld()
{
return "Hello World";
}
}
function CallService() {
$.ajax({
url: "WebService1.asmx/HelloWorld",
type: "GET",
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (data, textStatus) {
if (textStatus == "success") {
alert(data.d);
}
},
error: function (data, status, error) {
alert(error);
}
});
}
2. Use Ajax Script Manager call ASMX web methoddirectly
[System.Web.Script.Services.ScriptService]
public classWebService1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
}
3. Use JQuery Ajax call ASPX web method directly
publicpartial class About : System.Web.UI.Page
{
protected void Page_Load(object sender,EventArgs e)
{
}
[WebMethod]
publicstatic string HelloWorld()
{
return "Hello World 2";
}
function CallService() {
$.ajax({
url: "About.aspx/HelloWorld",
type: "POST",
dataType: "json",
contentType: "application/json; charset=utf-8",
data:"{}",
success: function (data, textStatus) {
if (textStatus == "success") {
alert(data.d);
}
},
error: function (data, status, error) {
alert(error);
}
});
}
CallService();
4. Use Ajax Script Manager call ASPX web method directly
publicpartial class About : System.Web.UI.Page
{
protected void Page_Load(object sender,EventArgs e)
{
}
[WebMethod]
publicstatic string HelloWorld()
{
return "Hello World 2";
}
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" >
</asp:ScriptManager>
<script type="text/javascript">
PageMethods.HelloWorld(OnCallSumComplete, OnCallSumError);
function OnCallSumComplete(result, txtresult, methodName) { alert(result); }
function OnCallSumError(error, userContext, methodName) { alert(error); }
</script>
5. Use Jquery call WCF directly
Add Ajax-Enable WCF Service
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode =AspNetCompatibilityRequirementsMode.Allowed)]
public classService3
{
// To use HTTP GET, add [WebGet] attribute. (Default ResponseFormat is WebMessageFormat.Json)
// To create an operation that returns XML,
// add [WebGet(ResponseFormat=WebMessageFormat.Xml)],
// and include the following line in the operation body:
// WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
[OperationContract]
public string DoWork()
{
// Add your operation implementation here
return "Do Work";
}
function CallService() {
$.ajax({
url: "Service1.svc/DoWork",
type: "POST",
dataType: "json",
contentType: "application/json; charset=utf-8",
data:"{}",
success: function (data, textStatus) {
if (textStatus == "success") {
alert(data.d);
}
},
error: function (data, status, error) {
alert(error);
}
});
}
OR
<%@ServiceHost
language=c#
Debug="true"
Service="WebApplication1.CalculatorService"
Factory=System.ServiceModel.Activation.WebScriptServiceHostFactory
%>
[ServiceContract(Namespace ="")]
[AspNetCompatibilityRequirements(RequirementsMode =AspNetCompatibilityRequirementsMode.Allowed)]
public classCalculatorService
{
[OperationContract]
public string DoWork()
{
// Add your operation implementation here
return "Do Work 2";
function CallService() {
$.ajax({
url: "Service2.svc/DoWork",
type: "POST",
dataType: "json",
contentType: "application/json; charset=utf-8",
data:"{}",
success: function (data, textStatus) {
if (textStatus == "success") {
alert(data.d);
}
},
error: function (data, status, error) {
alert(error);
}
});
}
- Ajax and ASP.Net Web Service
- Creating and Calling ASP.NET AJAX Web Service
- ASP.NET Ajax Web Service
- ASP.NET AJAX调用Web Service
- ASP.NET AJAX中使用Web Service
- Asp.net 2.0 Ajax调用Web Service
- [翻译]ASP.NET AJAX调用Web Service
- ASP.NET AJAX调用Web Service
- Asp.net AJAX Web Service写法差异
- Asp.net AJAX Web Service调试
- Send and Receive JSON objects to Web Service Methods using jQuery AJAX in ASP.Net
- Ajax & .Net Web Service
- [翻译]ASP.NET AJAX调用Web Service(超级好)
- jquery的ajax访问asp.net的web service
- ASP.Net Ajax框架 和ScriptMnager控件 调用Web Service
- ASP.NET Web Service
- ASP.NET Web Service
- ASP.NET Web Service
- 程序员玩游戏之二--篡改植物大战僵尸2的阳光值
- extern "C"的作用
- 素因子分解 Prime factorization
- Linux内核模块编程之helloworld
- iptables规则的查看、添加、删除和修改
- Ajax and ASP.Net Web Service
- 系统集成项目管理之项目风险管理
- 移动开发:iOS内存管理之:引用计数、ARC、自动释放池autoreleasepool和便捷方法之间的关系
- [Cocoa]用NSData读写二进制文件
- LeetCode-Largest Rectangle in Histogram
- 心得分享: 学习UEFI 之你把C语言学好了码?
- SQL 的from中嵌套的子查询的临时表名的作用域问题
- gdfg
- Qt新建线程的方法