Node & quot; Can't set headers after they are sent & quo

Source: Internet
Author: User

[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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.