Reputation: 101
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.
Upvotes: 2
Views: 927
Reputation: 62392
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":""}}
Upvotes: 6
Reputation: 359776
In addition to @jondavidjohn's answer (+1), you're also referencing the variable res
, which is not defined.
Upvotes: 1