2

I've got a page that will get a quick login check and access a web service for some JSON data. The data it's returning looks like this:

{ 'RESPONSE' : 'INVALID','CAMPAIGNID' : '0','MORELINK' : '' } 

The code I'm using to get this data and attempt to pop a modal window based on the information is:

   $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "Login.asmx/Logon",
            data: strData,
            dataType: "json",
            success: function (response) {
                $('#divResp').html(response.d);
                $(response.d).each(function(index,item){
                    alert(item.campaignid);
                });
                if (res.indexOf('invalid') >= 0){                       
                    //invalid account information UNP                       
                    foo = "You entered an invalid account number or password";
                    showDialog('screenActivate.asp',test_imgName);
                } 
                if (res.indexOf('none') >=0) {
                    //do not deliver splash screen
                    foo = "No splash screen for YOU!";
                } 
                if (res.indexOf('error') >=0) {
                    //general error
                    foo = "You entered an error";
                }
                if (res.indexOf('frozen') >=0) {
                    //member is locked out from numberous failed attempts
                    foo = "You have too many failed attempts.  Your account has been locked";
                }
                if (res.indexOf('.png') >=0) {
                    imgName = (response.d);
                    showDialog('screenActivate.asp',test_imgName);
                }
                alert(foo);
                $('#divResp').html(response.d);
            }
        });
        $('#divQuery').ajaxError(function(evt, request, settings){
            $('#divResp').html("Web services timeout.  Please try again.");             
        });
    }

The challenge is, either I get no information (Undefined) or as is with this current iteration, an error from Firebug that states uncaught exception: Syntax error, unrecognized expression: 'INVALID'

Am I not parsing the JSON response properly?

Relevant comment to answer

I pasted: "d":"{ 'RESPONSE' : 'INVALID','CAMPAIGNID' : '0','MORELINK' : '' } " into the jsonlint.com and that tool says the JSON is valid. When I attempt to loop through response.d with an each statement, it seems to go character by character.

1
  • 1
    Always validate the repsonse first e.g. with jsonlint.com. In your case you are definitely not getting a valid JSON. Commented Nov 21, 2011 at 21:40

2 Answers 2

6

JSON spec requires the use of double quotes (") instead of single quotes (')

Official JSON string parsing Diagram (from http://www.json.org)

http://www.json.org/string.gif

in response to your comment...

ok, here's the deal...

"d":"{ 'RESPONSE' : 'INVALID','CAMPAIGNID' : '0','MORELINK' : '' } "

IS valid JSON... but it is simply an object with a d property that contains the string...

"{ 'RESPONSE' : 'INVALID','CAMPAIGNID' : '0','MORELINK' : '' } "

data that would work the way you expect would look like this...

{"d": {"RESPONSE":"INVALID","CAMPAIGNID":"0","MORELINK":""}}
Sign up to request clarification or add additional context in comments.

9 Comments

I pasted: "d":"{ 'RESPONSE' : 'INVALID','CAMPAIGNID' : '0','MORELINK' : '' } " into the jsonlint.com and that tool says the JSON is valid. When I attempt to loop through response.d with an each statement, it seems to go character by character.
alright, I have the JSON properly coded, but I'm still not iterating through the object and getting name/value pairs. If I alert the return it just gives me Object [object] How do I determine the JSON contents? I'd like to know the campaignid, and if there is a morelink.
An object is not meant to be iterated over... just do something like this... if (response.d.MORELINK.length > 0) { alert(response.d.CAMPAIGNID); }
I'm getting a response.d.MORELINK is undefined error in Firebug. I'd offer to paste code, but it's exactly as you typed above. This is why I turned to Stack for help, because #$^* just isn't working as it should.
Sorry! forgot the wrapping curly braces... fixed it in the answer.
|
1

In addition to @jondavidjohn's answer (+1), you're also referencing the variable res, which is not defined.

1 Comment

I removed the declaration for res in troubleshooting the JSON data issue. It was right were the alert() was. :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.