如何从jquery调用.NET类方法 [英] How can I call a .NET class method from jquery

查看:72
本文介绍了如何从jquery调用.NET类方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些帮助。我知道如何使用jquery ajax调用Page方法/ Webservice方法,但我想知道如何调用.NET类方法以避免从处理程序/页面方法或服务方法复制代码。



我有一个用静态方法声明的类都用webmethod属性修饰。我已经尝试了但是我得到了一个错误500.我知道可以在MVC中调用一个动作方法,但我正在开发一个webforms项目,并且想在webforms中如何做。



任何帮助都将受到高度赞赏。



谢谢



我尝试过:



.Net Class

I need some help here. I know how to call a Page method/Webservice Method using jquery ajax but i would like to know how to call .NET class method to avoid copying codes from handler/page method or service method.

I have a class declared with static methods all decorated with webmethod attributes. I have tried it but i get an error 500. I know it's possible to call an action method in MVC but i'm working on a webforms project and would like to how to do it in webforms.

Any help would be highly appreciated.

Thanks

What I have tried:

.Net Class

<pre>
using System.Web.Services;
using System.Web.Script.Services;

public class Container
{
    public int Name { get; set; }
    public int Size {get;set;}

    [WebMethod()]
    [ScriptMethod()]
    public static bool Exists(string ContainerName)
    {
        // Check stuff here and return true or false;
        return true;
    }
}







Jquery Code






Jquery Code

$(document).ready(function () {
                            $('#testModel').click(function (e) {
                                // alert("calling web service now");

                                $.ajax({
                                    url: 'Models/Container/Exists',
                                    method: 'post',
                                    contentType: 'application/json; charset=utf-8;',
                                    data: '{ "ContainerName": "xxxx" }',
                                    success: function (r) {
                                        alert(r.d);
                                    },
                                    error: function (err) {
                                        alert(err.status + "\nScriptCall Failed");
                                    }
                                });
                            });
                        });

推荐答案

document )。ready ( function (){
(document).ready(function () {


' #testModel')。click( function (e){
// alert(立即调用Web服务);
('#testModel').click(function (e) { // alert("calling web service now");


.ajax({
url :' Models / Container / Exists'
方法:' post'
contentType:' application / json; charset = utf-8;'
data :' {ContainerName:xxxx}'
成功: function (r){
alert(rd);
},
错误: function (错误){
alert(err.status + \ nScriptCall失败);
}
});
});
});
.ajax({ url: 'Models/Container/Exists', method: 'post', contentType: 'application/json; charset=utf-8;', data: '{ "ContainerName": "xxxx" }', success: function (r) { alert(r.d); }, error: function (err) { alert(err.status + "\nScriptCall Failed"); } }); }); });


这篇关于如何从jquery调用.NET类方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆