Return Json-Objekt aus Asp.net webMethod zum Ajax-call

Ich habe folgenden Ajax call-Methode und Die asp.net webmethod.

Meine asp.net Funktion ist wieder einige Werte, die ich brauche, die wieder im Ajax-Aufruf ..

Ich habe versucht, viele Dinge, aber nicht geglückt ist, noch.

AJAX-CALL

<script type="text/javascript">


        $(document).ready(function () {
            //Add the page method call as an onclick handler for the control.
            $("#<%=ddlEmailTemplate.ClientID%>").change(function () {
                debugger;
                var myparam = $("#<%=ddlEmailTemplate.ClientID%>").val(); //id name for dropdown list
                $.ajax({
                    type: "POST",
                    url: "FileTax.aspx/ddlEmailTemplate_SelectedIndexChanged",
                    data: '{param:"' + myparam + '"}',
                    contentType: "application/json; charset=utf-8",
                    success: function (data) {
                        alert(data.d)
                    }
                });
            });
        });
    </script>

WebMethod asp.net

Aktualisiert, nachdem Sie Ihre Antwort

[WebMethod, ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = false)]
    public static string ddlEmailTemplate_SelectedIndexChanged(string param)
    {
        string subject;
        string Description;
        using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ForMyTaxConnectionString"].ConnectionString))
        {
            con.Open();
            DataSet ds = new DataSet();
            using (SqlCommand cmd = new SqlCommand())
            {

                cmd.Connection = con;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "Spo_ShowEmailTemplateContent";
                cmd.Parameters.Add(new SqlParameter("@Tid", SqlDbType.Int)).Value = Convert.ToInt32(param);
                using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                {
                    da.Fill(ds);
                    con.Close();
                    da.Dispose();
                }
                if (ds.Tables[0].Rows.Count > 0)
                {
                    DataRow dr = ds.Tables[0].Rows[0];

                    subject = Convert.ToString(dr["TemplateSubject"]);
                    Description = Convert.ToString(dr["TemplateDescription"]);

                }
            }

        }

        return JsonConvert.SerializeObject(new { subject = subject, description = Description });
        //return subject ;
Haben Sie überprüft, in der success Funktion, welche Daten bietet alert(data);
Ich bin nicht in der Lage, etwas zurückgeben von webmethod-also der Erfolg ist null
Sie müssen dataType:'JSON' als Sie erwarten json Ihre Daten ein und überprüfen Sie alert(data.subject)
Aber wie kann ich den Rückgabewert Thema hier ? das ist das Problem
JsonConvert.SerializeObject(new { subject = subject, description = Description }) hier, die Sie senden-Klasse als Json-Objekt, welches empfangen wird, in Daten

InformationsquelleAutor serious coder | 2017-03-07

Schreibe einen Kommentar