0

script.js

$('#loginform').submit(function(e){
 $.ajax({
 type:"POST",
 url:"/accounts/login/ajax/",
 data:$('#loginform').serialize(),
 success: function(msg){
 var msg = msg;
 msg = JSON.parse(msg);
 $('#messagestatus').html('');
 if(msg.username != null){
 $('#messagestatus').append(msg.username);
 }
 if(msg.password != null){
 $('#messagestatus').append(msg.password);
 }
 }
 });
 e.preventDefault(); 
});

returned object

{'username':['Required'],'password':['Required']}

When I am using JSON.parse and I am alerting, it's showing the correct error, but when I am appending it to the messagestatus div its not responding, please help .

Lukasz Koziara
4,3225 gold badges34 silver badges45 bronze badges
asked Aug 10, 2012 at 21:48
2
  • 3
    What's with the var msg = msg;? Commented Aug 10, 2012 at 21:51
  • jsonlint.com complains about {'username':['Required'],'password':['Required']} not being valid JSON. It comes back with Parse error on line 1: { 'username': [ -----^Expecting 'STRING', '}' Replace the ' with " and the JSON will be valid. You still got the issue though of password and username values being arrays. Commented Aug 10, 2012 at 21:59

3 Answers 3

2

Your username and password are arrays in your json.Either make it that your json looks like

 {'username':'Required','password':'Required'}

or change your script:

 if(msg.username.length > 0)
 {
 $('#messagestatus').text(msg.username[0]);
 }
 if(msg.password.length > 0)
 {
 $('#messagestatus').text(msg.password[0]);
 }
answered Aug 10, 2012 at 21:52
Sign up to request clarification or add additional context in comments.

2 Comments

but how do i check if username exists or not ??
add if ( typeof msg.username != 'undefined' ) before the first if, and if ( typeof msg.password != 'undefined') before the second if.
1

Might be significant, might not be, but your json object actually consists of sub-arrays:

{'username':['Required'],'password':['Required']}
 ^----------^ ^----------^

meaning that the decoded data structure in JS would actually be

obj['username'][0] and obj['password'][0]
answered Aug 10, 2012 at 21:53

1 Comment

It also has ' instead of " around its keys.
0

First of all, parameters do not need to be redeclared in their function.

then, to parse your json using $.parseJSON() :

msg = $.parseJSON(msg);

Your JSON contains arrays, use msg["username"][0] to access the first string and not the full array.

And I would put e.preventDefault(); as first statement before AJAX.

And .html('') => .empty() but it's the same result.

answered Aug 10, 2012 at 22:01

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.