zoukankan      html  css  js  c++  java
  • Node post请求 通常配合ajax

    //处理客户post请求
    //*1:加载相应模块 http express querystring
    //*2:创建web服务器
    //*3:监听端口8080
    const http = require("http");
    const express = require("express");
    const qs = require("querystring");

    var app = express();
    var server = http.createServer(app);
    server.listen(8080);

    //16:35--16:45
    //*4:创建表单 public/9/index.html [用户名密码]
    //*5:添加 jquery代码提交表单内容 post
    //*6:添加 get请求 /index /jquery
    app.get("/",(req,res)=>{
    res.sendFile(__dirname+"/public/9/index.html");
    });
    app.get("/jquery",(req,res)=>{
    res.sendFile(__dirname+"/public/9/jquery-1.11.3.js");
    });

    //7:接收post请求,获了用户数据
    app.post("/user",(req,res)=>{
    //接收客户端请求主体数据
    req.on('data',(buf)=>{
    console.log(buf);
    console.log(buf.toString());
    var obj = qs.parse(buf.toString());
    console.log(obj.uname);
    console.log(obj.upwd);
    res.send("<h1>ok</h1>");
    });
    });

    html中配合代码

    <script>
    $(function(){

    $("#btn").click(function(){
    $.ajax({
    type:"POST",
    url:'/user',
    data:{
    uname:$("#uname").val(),
    upwd:$("#upwd").val()
    },
    success:function(data){
    console.log("服务器端返回数据:"+data);
    }

    });

    });


    });

    </script>

  • 相关阅读:
    Tiddlywiki 维基程序使用手册
    Codeigniter 3.0 相关文档 part two
    css hack
    sql入门基础
    nodejs如何储存一个GBK编码的文件
    PHP 代码片段记录
    javascript 数字进制转换
    子网掩码计算题
    Trace文件过量生成问题解决
    PHP Header下载文件在IE文件名中文乱码问题
  • 原文地址:https://www.cnblogs.com/dianzan/p/7340246.html
Copyright © 2011-2022 走看看