One, the Get method is a common method used by Nodejs to receive data, passing through a URL
Parameters can be seen at the address (unsafe when logged in), and because the address bar controls the length of the parameter, the Get method cannot be a large data submission
Send data on page
function (Result) { console.log (Result.args); if (result.args== "OK") { //Receive JS return Data alert ("message success! ") } })
Receive data in JS
Router.get ("/action/zxly.js?",function(req,res) { //req.query: Gets the query parameter string for the URL var par=req.query; Res.send ({// send data to page })
Second, the Post method through the body to obtain parameters need to load the corresponding module
installation command:
NPM Install Body-parser
Add the appropriate module to the App.js
var bodyparser = require (' Body-parser '); // load This module in the body to get the parameter app.use (bodyparser.urlencoded ({extended:false})); // the argument is in string form
Form submission:
<formAction= "<%=basepath%>action/tedst.js"Method= "POST">User name:<inputtype= "text"name= "username" /> <BR/>Password:<inputtype= "Password"name= "pwd"/><BR/> <inputtype= "Submit"value= "Submit"/> </form>
JS Receive:
The Post method is required for safe submission of parameters such as user name, or for large data submissions
The Post method can only be submitted by the form and cannot be accessed through the Address bar.
Router.post ("/action/tedst.js",function(req,res,next)
Res.send ("----Post submitted successfully" +req.body.username+ " " + req.body.pwd); Next (); })
Three, Getjson receive requests across domains
JS in the return data:
Router.get ("/action/testgetjson",function(req,res,next) { var m= Req.query.jsoncallback; Res.write (M+ "({\" key\ ": \" abcdef\ "})"; // data Formats that are returned across domains // res.write ("{\" key\ ": \" abcdef\ "}"); Format of returned data in the same field res.end (); })
The page sends the request and receives the returned parameters
function Test () { $.getjson (function(data) { $ ("span"). Text (Data.key); }) }
Js:ajax the difference between post and get, the use of Getjson