0

I am trying to call a webmethod and get json object and display the data in aspx file with jquery. but something s wrong and it is not working. I will explain with the code below

here is the webmethod

Database db = DatabaseFactory.CreateDatabase("Connection String2");
        DbCommand dbCommand;
        dbCommand = db.GetStoredProcCommand("MedBul_Select_Selected_Professional");
        db.AddInParameter(dbCommand, "id", DbType.Int16, Convert.ToInt16(id));
        IDataReader dr = db.ExecuteReader(dbCommand);
        if(dr.Read())
        {
            int p_id = Convert.ToInt16(dr["ProfessionalID"].ToString());
            string firstname = dr["ProfessionalName"].ToString();
            string lastname = dr["ProfessionalSurname"].ToString();
            int prefix = Convert.ToInt16(dr["PrefixID"].ToString());
            int gender = Convert.ToInt16(dr["Gender"].ToString());
            string birthdate = dr["BirthDate"].ToString();
            string mobilephone = dr["MobilePhone"].ToString();
            string email = dr["Email"].ToString();
            string diplomano = dr["DiplomaNo"].ToString();

            return_str += "[{\"id\":\"" + p_id + "\",\"firstname\":\"" + firstname + "\",\"lastname\":\"" + lastname + "\",\"prefix\":\"" + prefix + "\",\"gender\":\"" + gender + "\",\"birthdate\":\"" + birthdate + "\",\"mobilephone\":\"" + mobilephone + "\",\"email\":\"" + email + "\",\"diplomano\":\"" + diplomano + "\"}]";
        }

and here is the jquery code.

  $('#btn_second').click(function () {
            //$('#txt_isim_4').val('test arif');
            $.ajax({
                type: "POST",
                url: "Registration.aspx/get_selected_professional",
                data: "{'id':'2'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    $.each(data, function (index, value) {
                        alert(value);
                        alert(value.d);
                        alert(index);
                        alert(value.firstname);
                    });

                }
            });
        });

I am trying to alert() what is returned but It doesnt display anything. I am very sure that I can get the data from Database and parse it properly....

What is wrong with my code? How can I get it done to display the json object?

8
  • Did you inspect the data and see whether data is present? Commented Mar 7, 2013 at 11:31
  • show your json results. Commented Mar 7, 2013 at 11:31
  • post your data object too... Commented Mar 7, 2013 at 11:31
  • check your browser console for any errors Commented Mar 7, 2013 at 11:31
  • how can I get the json object to display? I dont know how to inspect it? Commented Mar 7, 2013 at 11:32

2 Answers 2

2

seeing your response..

return_str += "[{\"id\":\"" + p_id + "\",\"firstname\":\"" + firstname + "\",\"lastname\":\"" + lastname + "\",\"prefix\":\"" + prefix + "\",\"gender\":\"" + gender + "\",\"birthdate\":\"" + birthdate + "\",\"mobilephone\":\"" + mobilephone + "\",\"email\":\"" + email + "\",\"diplomano\":\"" + diplomano + "\"}]";

no need to use each loop.. since in ajax you have mentioned dataType as json.. use . operator to get the objects

try this

success: function (data) {
              alert(data.id);
              alert(data.firstname); // similar for others
            }
Sign up to request clarification or add additional context in comments.

2 Comments

as its array of objects shouldn't it be like data[0].id
no... the dataType:'json' of ajax will automatically parse it for you.. try it out.. :)
0

i assume the returned json looks like

var $json ='[{"id":"1","name":"john"},{"id":"2","name":"smith"}]';
var json = $.parseJSON($json);//will already be parsed in your callback function bcoz of the dataType:json

try modifying your loop like

$.each(json,function(i,j){
    $(j).each(function(k,v){
     console.log(v.id);
     console.log(v.name);
    });
});

http://jsfiddle.net/Eu9Pp/

Comments

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.