1

I have code that generates a JSON string. An example string that is generated by that code is as follows. I have validated it through the JSON lint tool and it validates perfectly.

{"ShipToName" : "(    ) CARPINTERIA         CA", "ShipToAddress1" : "3785 SANTA CLAUS LANE", "ShipToAddress2" : "", "ShipToCity" : "CARPINTERIA", "ShipToState" : "CA", "ShipToZip" : "93013", "ShipVia" : "UPS", "Terms" : "01", "SalesRep" : "KV1" }

I then have some JQuery that is going to parse that string. Right now I am just trying to alert one of the parts of the string to be sure the code is working correctly. I have tried both of the following with no success:

Attempt #1: Alerts 'undefined'

function hidShipTo_IndexChanged(sender, args) {
                var strComboID = $find("<%=rcbCustomer.ClientID%>");
                var strValue = strComboID.get_value();
                var strShipToVal = $("#hidShipTo").val();
                var strData = "{ strSoldToSelected: '" + strValue + "', strShipToSelected: '" + strShipToVal + "' }";
                alert("yes");
                $.ajax({
                    type: "POST",
                    url: "/webservices/ProductServer.asmx/PopulateShipToDetails",
                    data: strData,
                    contentType: "application/json; character=utf-8",
                    dataType: "json",
                    success: function (msg) {
                        alert(msg.SalesRep);
                    },
                    failure: function (xhr, ajaxoptions, thrownError) {
                        alert("Error1:" + xhr.status);
                        alert("Error2:" + thrownError);
                    }
                });
            }

Attempt #2: throws an error inside of a JQuery library object

function hidShipTo_IndexChanged(sender, args) {
                var strComboID = $find("<%=rcbCustomer.ClientID%>");
                var strValue = strComboID.get_value();
                var strShipToVal = $("#hidShipTo").val();
                var strData = "{ strSoldToSelected: '" + strValue + "', strShipToSelected: '" + strShipToVal + "' }";
                alert("yes");
                $.ajax({
                    type: "POST",
                    url: "/webservices/ProductServer.asmx/PopulateShipToDetails",
                    data: strData,
                    contentType: "application/json; character=utf-8",
                    dataType: "json",
                    success: function (msg) {
                        alert(msg.SalesRep);
                    },
                    failure: function (xhr, ajaxoptions, thrownError) {
                        alert("Error1:" + xhr.status);
                        alert("Error2:" + thrownError);
                    }
                });
            }

Any ideas on how I can access each of the items in the JSON String?

Thanks!

2
  • success: function (msg) { console.log(msg); alert(msg.SalesRep); Does it show what you expect? Commented Feb 19, 2014 at 20:08
  • So, the JSON string is inside strShipToVal variable? And you want to access the individual elements? Commented Feb 19, 2014 at 20:08

3 Answers 3

1

since your code does not throw an undefined exception, then we can deduce 'msg' is not undefined.

Use a debugger to see what your msg really contains:

alert(msg.SalesRep);
console.log(msg);

note: contentType should be 'charset=utf-8', not 'character=utf-8'. also the string you're sending is not valid json. It should be double quoted instead of using single quotes.

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

5 Comments

Hi, thanks for that tip, but the problem is on the return of the already validated JSON string as listed in the question, not on the string being passed in.
Here is the string that goes into the console: d: "{"ShipToName" : "(0001) MIAMI FL", "ShipToAddress1" : "PTY 7966", "ShipToAddress2" : "8610 NW 72ND STREET", "ShipToCity" : "MIAMI", "ShipToState" : "FL", "ShipToZip" : "33166", "ShipVia" : "SRG", "Terms" : "01", "SalesRep" : "999" }"
You validated my answer, does that mean your problem is solved ? if so, you could edit your question or comment to explain what the problem really was.
@MichaelMahony nevermind me, didn't saw you answered already.
I marked you as the answer because your answer led me to what the problem was. Thanks!
1

So the answer was really simple once I checked the console log. I added this line of code and I was able to begin accessing everything in the JSON string:

var obj = $.parseJSON(msg.d);

Comments

0

This makes no sense:

var strData = "{ strSoldToSelected: '" + strValue + "', strShipToSelected: '" + strShipToVal + "' }";                

It should just be

var strData = { strSoldToSelected: strValue, strShipToSelected: strShipToVal };

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.