Js:ajax the difference between post and get, the use of Getjson

Source: Internet
Author: User

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

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.