美文网首页我爱编程
jQuery ajax-post()方法

jQuery ajax-post()方法

作者: 不睡觉呀 | 来源:发表于2018-04-04 20:48 被阅读0次
    • jQuery ajax-post();

    $.post("2.json",{"name":999999999999},function(results){
    
                $("div").html(results);
            });
    

    通过ajax向服务请求改变div中的文本;

    var http=require("http");
    var urlW=require("url");
    var fs=require("fs");
    var path=require("path");
    var qs=require("querystring");
    var server=http.createServer(process);
    server.listen(8080);
    
    function process(req,res){
        var url=req.url;
        console.log(url);
        if(url=="/"||url=="./1.html"){
            var file="1.html";
            getFile(file,res);
        }else if(url =="/2.json"){
            console.log(req.method);
            console.log('-----');
            if(req.method=="POST"){
                var content="";
                req.on("data",function(data){
                    content+=data;
                });
                req.on("end",function(){
                    var object=qs.parse(content);
                    if(object.name==666){
                        res.end("66666666");
                    }else{
                        res.end("who         ");
                    }
                });
            }
        }
        else if(url.indexOf(".json")>=0){
            var list=[9,8,7,6,5,4,3,2,1,0];
            var object={"list":list};
            res.writeHead(200,{"Content-type":"application/json"});
            res.end(JSON.stringify(object));
    
        }else if(path.extname(url)==".html"){
            var fileName=url.substring(1);
            getFile(fileName,res);
    
        }else{
            var aa=urlW.parse(url);
            var para=aa.query;
            var object=qs.parse(para);
            if(object.name==888){
                res.end("66666");
            }else{
                res.end("who tm cares");
            }
        }
    }
    function getFile(file,res){
        var rs=fs.createReadStream(file);
        rs.pipe(res);
    
        rs.on("error",function(){
            res.end("error");
        });
    }
    

    其中

    else if(url =="/2.json"){
            console.log(req.method);
            console.log('-----');
            if(req.method=="POST"){
                var content="";
                req.on("data",function(data){
                    content+=data;
                });
                req.on("end",function(){
                    var object=qs.parse(content);
                    if(object.name==666){
                        res.end("66666666");
                    }else{
                        res.end("who         ");
                    }
                });
            }
        }
    

    对传过来的参数进行了处理,如果发来的是post请求,则对请求中的参数进行处理,并且将参数转换为对象,通过对象的属性实现对它的操作。

    相关文章

      网友评论

        本文标题:jQuery ajax-post()方法

        本文链接:https://www.haomeiwen.com/subject/xnylhftx.html