0

I am trying to return the list of object from server side code through ajax. It is returning successfully, But I cannot extract.

Jquery Code

$.ajax({
            type: "POST",
            contentType: "application/json;charset=utf-8",
            url: "MyWebService.asmx/Execute_SequenceNo",

            data: JSON.stringify({
                "journalEntry": $('#ContentPlaceHolder1_txtJournalEntry').val().trim()
            }),
            dataType: "json",
            success: function (data) {
                alert("Success = " + data.d.length + ', Data 0 =' + data.d[0] + ' ' + data.d[1])
            },
            error: function (result) {


            }
        })

The below code to get the records from my Sql Server

WebService

[WebMethod]
    public SequenceNumber[] Execute_SequenceNo(string journalEntry)
    {
        DataTable dt = new DataTable();
        List<SequenceNumber> details = new List<SequenceNumber>();
        try
        {
            string cs = ConfigurationManager.ConnectionStrings["TestConnectionString"].ConnectionString;

            using (SqlConnection con = new SqlConnection(cs))
            {
                SqlCommand cmd = new SqlCommand("spTest", con);
                cmd.CommandType = CommandType.StoredProcedure;


                con.Open();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dt);
                foreach (DataRow drow in dt.Rows)
                {


                    sequence.errorMsg = "Valid";
                    details.Add(sequence);
                }
            }
            return details.ToArray();
        }
        catch (Exception ee)
        {
            //return "NotValid error = " + ee.ToString();
            SequenceNumber sequence = new SequenceNumber();
            sequence.errorMsg = "NotValid";
            return details.ToArray();
        }


    }

it return successfully, but my alert is like below

Output

Success = 2, Data 0 =[object Object] [object Object]
2
  • 1
    That alert looks reasonable to me. What's the actual problem? What are you expecting the alert to show and why? Side note: Don't use alert for debugging, use console.log. It's non-blocking, serializes complex types, maintains a running log, etc. Commented Jan 3, 2019 at 12:14
  • @David how to print the value of sequence.journalEntry? Commented Jan 3, 2019 at 12:16

1 Answer 1

1

Your code appears to be working fine. It just seems that you're expecting alert() to do something more than it does. It doesn't serialize objects or display any given property that you might want from those objects. Anything that's not a primitive value just gets displayed as:

[object Object]

The simplest approach for your debugging is to not use alert(). Instead, use console.log(). This has a few benefits:

  • It's non-blocking, so the code can continue to execute with normal timing. This reduces the possibility of timing bugs caused by code behaving very differently in debug vs. release scenarios.
  • It serializes complex types, so you can fully examine your objects.
  • It's not a dismissable dialog but instead an ongoing log.

Open your browser's debugging tools and take a look at the console. Then in your code (in your success function where you currently use alert()) simply do something like:

console.log("Success = ", data);

Observe what's logged to the console. For debugging this will give you an exact representation of what the complex object/array data is, what it's elements are, etc. From there you can observe specifically how to access any given value within that object/array.

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

4 Comments

I tried with console. it prints Success = [object Object] the same
@Liamneesan: Can you update your code to show what you tried?
@Liamneesan: You're still using alert(). Use console.log() instead. (Refer to the answer above.) Also, if your web service is returning XML, I would expect the client-side code to fail entirely as it's looking for JSON. Put some debugging output in the error function as well, and observe any errors on the browser's development console.
Sorry, your code is working, Actually I tried like this console.log("Success returns = " + data + ', Data 0 =' + data.d[0].toString() + ' ' + data.d[1]);, then it returns the same what I mentioned on my previous comment. But your code is working. console.log("Success = ", data);Thanks bro

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.