简体   繁体   English

ajax在asp.net中返回未定义的对象

[英]ajax is returning an undefined object in asp.net

here is the getter method in c# it gets the meanings of a word input by a user by a query and fills them in a data table afterwards a list of strings will be filled by a loop which will contain those meanings which is at last converted and return as json string : 这是C#中的getter方法,它通过查询获取用户输入的单词的含义,并将其填充到数据表中,然后用循环填充字符串列表,该循环将包含这些含义,最后将这些含义转换并返回为json字符串:

             public static string  getmeanings(string word)
         {
    string cs = 
            ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;

                        List<string> stringArr = new List<string>();

    using (SqlConnection con = new SqlConnection(cs))
    {

        con.Open();
        using (SqlDataAdapter rdr = new SqlDataAdapter("SELECT MEANING FROM WORDS T1 , MEANINGS T2 WHERE WORD LIKE N'" + word + "'AND T1.WORD_ID = T2.WORD_ID", con))
        {
            using (DataTable dt = new DataTable())
            {

                rdr.Fill(dt);
                for(int i =1;i< dt.Rows.Count; i++)
                {
                    stringArr.Add(dt.Rows[0][i].ToString());

                }

            }

            string json1 = JsonConvert.SerializeObject( stringArr);
            return json1;

        }
    }
}

here is the ajax/jquery code : on keyup the ajax will call the getter method and at success it will alter a table that contains the result of the return json object 这是ajax / jquery代码:在keyup上,ajax将调用getter方法,成功后,它将更改包含return json对象结果的表

       $(function () {

        $("#text1").keyup( function () {
            var word = $("#text1").val();
            $.ajax({
                type: "GET",
                url: "toshow.aspx/getmeanings",
                data: { word: word },
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (result) {




                $(".tablefill").append("<table><tr><td>meaning id</td><td>meaning</td></tr><tr><td>"+result[0]+"</td></tr></table>");
                    console.log(result);
                }, error: function (err) {
                    alert('ERROR');

                }
            });
        });
    });

after i test the code ajax is succeeding , no error or failure but the result is always an undefined object in console log 在我测试代码ajax成功之后,没有错误或失败,但是结果始终是控制台日志中的未定义对象

console.log(result.success);

Are you sure 'success' is a member of 'result'? 您确定“成功”是“结果”的成员吗? Perhaps try see what console.log(result) does. 也许尝试看看console.log(result)的作用。

You can start from the browser and debug backwards. 您可以从浏览器开始,然后向后调试。

Open your developer console (F12 for chrome), go to the Network tab and then let Ajax fire again. 打开您的开发者控制台(适用于chrome的F12),转到“网络”标签,然后再次让Ajax启动。

Then you will most likely notice that the server replied with a 200 response, just no content. 然后,您很可能会注意到服务器回复了200,只是没有内容。

Now you start and work from your returned result back to your query. 现在,您可以开始并将返回结果返回查询。

My bet would be that it could likely be that 0 rows were returned and that your loop didn't populate stringArr. 我敢打赌,很可能返回了0行,并且您的循环没有填充stringArr。 You might get another error once your query returns some rows as I am a bit uncertain about the multidimensional array. 一旦查询返回一些行,您可能会遇到另一个错误,因为我对多维数组不确定。 Looks like you might be trying to loop through an equal amount of columns as you have rows. 看起来您可能正在尝试遍历相等数量的列(因为您有行)。

I am certain you will fix the errors occurring in your Code Behind once they start showing up. 我敢肯定,一旦它们显示出来,您就会解决在代码隐藏中出现的错误。

The result should be an array of items from your database. result应该是数据库中的一组项目。 result.success would always be undefined, there is no .success on your returned object. result.success始终是未定义的,返回的对象上没有.success

On another topic, you'll want to take care of your SQL Injection problem. 在另一个主题上,您需要解决SQL注入问题。 ( https://en.wikipedia.org/wiki/SQL_injection ) https://en.wikipedia.org/wiki/SQL_injection

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

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