0

I'm getting the following error when try to read json data from asp.net function, here is the image error enter image description here

Here is the jQuery code,

<script type="text/javascript">
    $(document).ready(function () {
        $("#getdata").click(function () {
            $.ajax({
                type: "POST",
                url: "Default.aspx/GetData",                   
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    alert(data.d);
                    // $("#company").html(data.d);
                      console.log(data);
                },
                error: function (error) {
                    alert(error);
                }
            });
        });
    });
</script>

what wrong I did to get this error. Appreciate your any help. getting this error from console log,console.log(error) enter image description here

8
  • What you return from asp.net method? Commented Jul 5, 2013 at 15:35
  • it's result not an error, what your console.log says in success? Commented Jul 5, 2013 at 15:35
  • That's because alert() is not a debugging tool, you should be using the console. Commented Jul 5, 2013 at 15:35
  • Do console.log(JSON.stringify(data.d)) and check whats in the console. Commented Jul 5, 2013 at 15:36
  • Try alert(data.d[0]); Commented Jul 5, 2013 at 15:36

3 Answers 3

2

The reason you are seeing that is that data.d is an object representing the returned JSON text response. When you pass in an object into alert, it displays [object Object]. Whatever you are looking for will be a property of data.d. I would put a breakpoint on that line and see what properties are available. It'll be something like data.d.myProperty which will contain the actual string / HTML you are trying to work with.

Sign up to request clarification or add additional context in comments.

Comments

0

dont use alert! That will show this only. Since your response is a Json Object, please try to JSON.stringify it or use console.log to view the data

6 Comments

I have added the "console.log(JSON.stringify(data.d))" on Success and error section, but not getting anything from Chrome Console
just do console.log(data) like this: success: function (data) { console.log(data); }
its not showing nothing, do I need to do any settings in Chrome?
ctrl+shift+I: this will open the developers terminal> please open the networking tab, then refresh the page, you can then see the server side response coming back. please check the response there
In response tab i could see the same codes for Default.aspx page
|
0

Finally, I found what was the issue, I have to add "[WebMethod()]" declaration just before the asp.net function. For example,

    [WebMethod()]
    public static string GetData()
    {

        try
        {
            string strjson;
            string connectionstring = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
            SqlConnection Con = new SqlConnection(connectionstring);
            DataSet DS = new DataSet();
            String CmdText = "Select Compname,compadd1,compadd2,compemail from aas_company where compid=@cmpid";
            SqlCommand cmd = new SqlCommand(CmdText, Con);
            cmd.Parameters.Add("@cmpid", SqlDbType.Int).Value = 22;
            Con.Open();
            SqlDataAdapter DA = new SqlDataAdapter(cmd);
            DA.Fill(DS);
            DataTable dTable = DS.Tables[0];
            strjson = GetJSONString(dTable);
            Con.Close();               
            return strjson;
        }
        catch (Exception ex)
        {
            throw new System.Exception("Error In Get Data" + ex.Message);
        }
    }

This answer not for experts only for just beginners, thank you.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.