error handling - Correct way to respond to client on Node.js? -
This is my first attempt on creating a server ...
I have installed a server Handle contact form submissions, including predefined captcha strings.
When the server receives a contact form, then the captcha string is expected, so I want it to be only JSON If the captcha string is wrong, then I want the server to say "Captcha" was incorrect, so ask the client user to try again. But I do not know how to do it. On the server: On Customer: When I use How should I react to the server so that the "error" part of the AJAX request on the client can be executed? or "Error" part is considered to handle cases where 'do not react at all' in the server, that is, when there is something wrong server leads to server, like the exception, there is a real error, And in such cases where my evaluation on the server is not the expected result? On the server: On Customer: I think what you have to do Here's an example: See for more information. response.end (JSON.stringify (parsedURL) of the parsed contact )) Using the query form;
var httpServer = http.createServer (function (request, response) {if (/// contactform \ ?. ..... /. Test (request.url)) {var parsedURL = url.parse (request.url, true); var name = parsedURL.query.name; var email = parsedURL.query.email; var topic = ParsedURL.query.subject; var Inquiry = parsedURL.query.enquiry; var CAPTCHA = PERSEAD URL.query.captcha; if (CAPTCHA! == "TEST") {// Send a "bad" response to the client and message "bad Captcha "} and responses .and (JSON.stringify (parsedURL.query));}}). Listen (8080);
$ Ajax ({url: "/ contact form ....... .......", success: function (msg) {console.log (msg);}, error: function (msg) {// Should the "bad captcha" response be handled properly here? Console.log (Msg); // that should be equal to console.log ("bad captcha");}});
response.end (JSON.stringify (parsedURL)); Customer (jQuery) assumes that "success"
var httpServer = http CreateServer (function (request, response) {if (///contactform\?....../.test(request.url)) {var parsedURL = url.parse (request Url, true); var name = parsedURL .query.name; var email = parsedURL.query.email; var topic = parsedURL.query.subject; var inquiry = parsedURL.query.enquiry; var captcha = parsedURL.quer y.captcha; var response = JSON.stringify (parsedURL) .query); if (captcha! == "test") response = "bad captcha"; response.end (response);}}). Listen (8080);
$ Ajax ({url: "/ contact form ....... .......", success: function (msg) {console.log (msg); // msg like either straared object or "bad captcha "Ho.}}}); In other words, when a request is successfully received on the server, but the server wants to know whether something is missing or whatever, then the form of feedback from the server. Should be sent in "error" (i.e. the client's AJAX code controlled by the "error" block) or with the proper message as "success" what exactly is that happening?
set your response headers
var body = 'Sorry!'; Response.writeHead (404, {'content-length': body.length, 'content-type': 'text / plain'});
Comments
Post a Comment