简体   繁体   中英

Ajax jQuery getting Error: [object Error]

Hi i try to send a parameter to my Webservice to get the Data. But iam getting always an Error: [object Error]

Error-Snapshot

What does that Error means?

The Webservice is working so far. If i Invoke the Webmethod in the Browser i get all the data.

What iam doing wrong. I have tried many things, but nothing helped.. Hope you can help me

My Query:

    function loadDate() {

        jQuery.support.cors = true;
        PanelID = document.getElementById('PanelID').value;
        alert(PanelID);


        jQuery.ajax({
            type: "POST",
            url: "http://nexxt-entwicklung.de/Web/Service1.asmx/getDatetime",
            data: "{ 'PanelID': '" + PanelID + "' }",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {
                alert("Success: " + msg.d);

            },
            error: function(msg) {
                alert("Failed: " + msg.status + ": " + msg.statusText);

            }
        });   
    }

My Webservice:

 [ScriptService]
public class Helper
{
    public class VERANSTALTUNGEN
    {
    public string Von { get; set; }
    public string Bis { get; set; }
    public string Thema { get; set; }
    public string PanelIDs { get; set; }
    }

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public static List<VERANSTALTUNGEN> getDatetime(string PanelID)
        {

            List<VERANSTALTUNGEN> Besprechungen = new List<VERANSTALTUNGEN>();

            StringBuilder query = new StringBuilder("SELECT DISTINCT r.PanelID AS PANEL_ID, rr.Von AS DATEVON, rr.Bis AS DATEBIS, b.THEMA AS BESPRECHUNGSTHEMA FROM RAUM r right join RESERVIERUNGRAUM rr ON r.ID = rr.Raum_ID right join BUCHUNG b ON rr.BUCHUNG_ID = b.ID where r.PANELID = @ID ORDER BY rr.VON");

            using (SqlConnection con = new SqlConnection(@"Data Source=Localhost\SQLEXPRESS;Initial Catalog=BOOK-IT_LH;Integrated Security=true;"))
            using (SqlCommand cmd = new SqlCommand(query.ToString(), con))
            {
                cmd.Parameters.Add("@ID", System.Data.SqlDbType.Char);
                cmd.Parameters["@ID"].Value = PanelID;
                con.Open();

                using (SqlDataReader rdr = cmd.ExecuteReader())
                {
                    while (rdr.Read())
                    {
                        if (rdr["DATEVON"] != DBNull.Value && rdr["DATEBIS"] != DBNull.Value)
                        {
                            Besprechungen.Add(new VERANSTALTUNGEN()
                            {
                                Von = rdr["DATEVON"].ToString(),
                                Bis = rdr["DATEBIS"].ToString(),
                                Thema = rdr["BESPRECHUNGSTHEMA"].ToString(),
                                PanelIDs = rdr["PANEL_ID"].ToString()
                            });
                        }
                    }
                }
            }
            return Besprechungen;
     }
}

Webservice Method

[System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {
        [WebMethod]
        public List<Helper.VERANSTALTUNGEN> getDatetime(string PanelID)
        {
            return Helper.getDatetime(PanelID);
        }
    }

It means that msg.statusText is an object that is an instance of Error .

Use console.log instead of alert to see what the object contains in a console in Chrome/Firebug, or loop through the object and alert each key/value.

When it comes to the problem itself, while using dataType json:

you shouldn't do:

data: "{ 'PanelID': '" + PanelID + "' }",

but rather something like

data: { 'PanelID': PanelID },

cause otherwise instead of posting some object you're sending string

do always make sure you read the specs: http://api.jquery.com/jQuery.ajax/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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