Solved

working with JSON data with multiple items returned

Posted on 2013-10-23
3
281 Views
Last Modified: 2013-10-24
I'm trying to return multiple items in my JSON call and place that data into multiple div's

This is my code to return the Json

     
 string JSONstring = "[";
        foreach (PersonInformation PS in PList)
           {
               JSONstring = JSONstring + "{";
               JSONstring = JSONstring + "EmpName:";
               JSONstring = JSONstring + "'" + PS.EmpName + "',";
               JSONstring = JSONstring + "PhoneNumber:";
               JSONstring = JSONstring + "'" + PS.Number + "'";
               JSONstring = JSONstring + "}";
           }
        JSONstring = JSONstring + "]";

Open in new window


This is my jquery

 $(document).ready(function () {
             $("#<%=txtEmployeeID.ClientID%>").blur(function () {
                 currentObject = $("#<%=txtEmployeeID.ClientID %>").val();
                 $.ajax({
                     type: "POST",
                     url: "EmployeeData.asmx/GetEmployeeInfo",
                     data: "{employeeID:'" + currentObject + "'}",
                     contentType: "application/json; charset=utf-8",
                     dataType: "json",
                     success: function (msg) {
                         BindEmployee(msg.d)
                     },
                     error: function (xhr, ajaxOptions, thrownError) {
                         alert(xhr.status);
                         alert(thrownError);
                     }

                 });

             });

             function BindEmployee(msg) {
                 $.each(msg, function () {
                     var EmpName = msg.d[0];
                     var Phone = msg.d[1];
                     $("#EmployeeName").html(EmpName);
                     $("#PhoneNumber").html(Phone);

                 });
             }
        });

Open in new window


what am I doing wrong?
0
Comment
Question by:NickMalloy
3 Comments
 
LVL 12

Expert Comment

by:zappafan2k2
ID: 39595476
You haven't told us what isn't working - meaning are you getting an error message, etc.

That said, take a look at your output.  There's probably no comma in between the objects.  Not sure what language that is, but can you build an array and use some sort of join function?  Otherwise, if you put the comma at the end of your foreach, you'll end up with a final comma, which will screw things up.
0
 

Author Comment

by:NickMalloy
ID: 39595517
It returns a string, but I can't break it up into my fields. I'm working on a asp.net c# page using jquery.

My response from my ajax call is this.

"[{\"EmpName\":\"Nick Malloy\",\"Phone\":\"96325\"}]"
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 400 total points
ID: 39595646
Test page : http://jsfiddle.net/yjpJq/

          var a = "[{\"EmpName\":\"Nick Malloy\",\"Phone\":\"96325\"}]";
          a = $.parseJSON(a);
          BindEmployee(a);

            function BindEmployee(msg) {
                     var EmpName = msg[0].EmpName;
                     var Phone = msg[0].Phone;
                     $("#EmployeeName").html(EmpName);
                     $("#PhoneNumber").html(Phone);
             }

Open in new window

0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
delete database record with modal 21 34
Css issue on scroll 1 20
update field on focusout 15 24
Syntax error in this jQuery codes 6 9
There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now