[Javascript]
$. Post ("/index? C = signUp ", {v:
{
"Username": $ ($ inputValue [0]). val (),
"Nick_name": $ ($ inputValue [1]). val (),
"Email": $ ($ inputValue [2]). val (),
"Password": $ ($ inputValue [3]). val (),
"Repassword": $ ($ inputValue [4]). val ()
}
},
Function (data ){
If (data. code = 0 ){
Window. location. href = "/index? C = emailPage & email = "+ data. data. email;
} Else if (data. code =-1 ){
$ (". ErrorMsg"). eq (4). show ();
} Else if (data. code =-2 ){
$ (". ErrorMsg"). eq (8). show ();
}
If (data. code =-3 ){
$ (". ErrorMsg"). eq (1). show ();
}
}, 'Json ');
Code 1-1
After I post to an action, the action returns the following data to me:
[Javascript] view plaincopyprint?
Res. send ({"code": 0, "msg": "login success", "data": dataJson });
Code 1-2
Since there are no return statements in the 12 lines in code 1-1, the post has not been fully executed yet and we need to continue the execution, and we have already jumped to the new url!
The above solution is to add return under the 12 rows of code 1-1 after the jump (that is, add a return when redirt;
[Javascript]
$. Post ("/index? C = signUp ", {v:
{
"Username": $ ($ inputValue [0]). val (),
"Nick_name": $ ($ inputValue [1]). val (),
"Email": $ ($ inputValue [2]). val (),
"Password": $ ($ inputValue [3]). val (),
"Repassword": $ ($ inputValue [4]). val ()
}
},
Function (data ){
If (data. code = 0 ){
Window. location. href = "/index? C = emailPage & email = "+ data. data. email;
Return;
} Else if (data. code =-1 ){
$ (". ErrorMsg"). eq (4). show ();
} Else if (data. code =-2 ){
$ (". ErrorMsg"). eq (8). show ();
}
If (data. code =-3 ){
$ (". ErrorMsg"). eq (1). show ();
}
}, 'Json ');
Note that this problem is not only caused by js errors in the background, but may also occur in front-end js!
Typical error instance:
Return is not used to return data, but to interrupt function execution. The following code is 1-3:
[Javascript]
If (err ){
Res...
} Else {
Res...
}
Code 1-3 May cause node "Can't set headers after they are sent ".
But there is no problem if you modify the following code 1-4:
[Javascript] view plaincopyprint?
If (err) return res...
Res...
Author: danhuang2012