前端向后端发送数据请求
form
ajax
jsonp
请求方式
Get--->数据在url中(百度搜索)
Post--->数据不在url中(淘宝登录)
form表单,以get形式提交至http://localhost:8000/NP
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form action="http://localhost:8000/NP" method="get">
用户名:<input type="text" name="user" value=""><br/>
密码:<input type="password" name="pwd" value=""><br/>
<input type="submit" value="提交">
</form>
</body>
</html>
get.js实现对url的解析
querystring模块实现
var http=require('http');
var querystring=require('querystring');
http.createServer(function(request,response)
{
if(request.url!="/favicon.ico")
{
var GET={}
var arr=request.url.split('?')
var url=arr[0]
/*var arr2=arr[1].split('&')
var arr3=arr2[0].split('=')
var arr4=arr2[1].split('=')
GET[arr3[0]]=arr3[1]
GET[arr4[0]]=arr4[1]*/
GET=querystring.parse(arr[1])//等效于上述/**/内部分
console.log(url,GET)// /NP {user:'xxx',pwd:'xxxxx'}
response.end();
}
}).listen(8000);
console.log('Server running at http://127.0.0.1:8000/');
url模块实现
var http=require('http');
var urlib=require('url');
http.createServer(function(request,response)
{
if(request.url!="/favicon.ico")
{
var arr=urlib.parse(request.url,true)
var url=arr.pathname //路径名
var GET=arr.query //数据集合
console.log(url,GET)// /NP {user:'xxx',pwd:'xxxxx'}
response.end();
}
}).listen(8000);
console.log('Server running at http://127.0.0.1:8000/');