简体   繁体   English

如何使用ajax在aspx.cs文件中触发方法

[英]How to fire a method in aspx.cs file using ajax

I wanna call a method on PersonalDetails.aspx.cs page using Ajax . 我想使用AjaxPersonalDetails.aspx.cs页面上调用一个方法 I have tried to it using following code. 我尝试使用以下代码。 but it doesn't worked. 但它不起作用。 I just wanna fire the method included in the PersonalDetails.aspx.cs How do i do it? 我只是想解雇PersonalDetails.aspx.cs中包含的方法我该怎么办? :) Can somebody help me. :)有人可以帮助我。

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>


 <script type="text/javascript">
    $(document).ready(function () {
        $("input").keydown(function () {
            $("input").css("background-color", "yellow");
        });
        $("input").keyup(function () {
            $("input").css("background-color", "pink");

            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "PersonalDetails.aspx/GetFarmersByName",
                data: { name: +request.term },
                dataType: "json",
                async: true,
                dataFilter: function (data) { return data; },
                success: function (data) {
                    return data;
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert(textStatus);
                    alert(errorThrown);
                }
            });
        });
    });
</script>

This is The method I wanna fire.. 这是我想解雇的方法..

    [WebMethod]
    [ScriptMethod]
    public  bool GetFarmersByName(string name)
    {
        _personalData = new personalData();
        int cky = 45;
        CdMa cdMas = new CdMa();
        cdMas = _personalData.getcdMasByConcdCd2(cky, "AdrPreFix", true);
        int prefixKy = cdMas.CdKy;

        List<FMISPersonalDataViewByName_Result> list = new List<FMISPersonalDataViewByName_Result>();

        list = _personalData.GetPersonalDataByName(prefixKy, cky, name);

        if (list != null)
        {
            grvPersonalData.DataSource = list;
            grvPersonalData.DataBind();
            return true;
        }
        return false;

    }

Aarif Qureshi answer should be the accepted one. Aarif Qureshi的回答应该是被接受的。

If you run what you're running, chances are you're getting " {"Message":"Invalid JSON primitive:... "? It looks like your data object is malformed, it should be data: "{...}" OR data: JSON.stringify({ ... }) 如果你运行你正在运行的东西,很可能你得到“ {”消息“:”无效的JSON原语:...... “?看起来你的数据对象格式不正确,它应该是数据:”{... “OR数据:JSON.stringify({...})

( which by the way was also incorrect - and it was already pointed out ) 顺便说一句也是错误的 - 已经指出了

please make your method GetFarmersByName to public static bool GetFarmersByName and try to debug the code 请将您的方法GetFarmersByName设置为public static bool GetFarmersByName并尝试调试代码

and change following 并改变以下

data: "{name: " + request.term + "}" , data: "{name: " + request.term + "}"

[WebMethod]
[ScriptMethod]
public  bool GetFarmersByName(string name)

this method must be public static method which return some data if you want to call it by ajax.. 此方法必须是公共静态方法,如果要通过ajax调用它,则返回一些数据。

like 喜欢

[WebMethod]
[ScriptMethod]
public static bool GetFarmersByName(string name)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM