账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    node后台ajax数据传输问题
    • 2017-09-10 00:00
    • 10
    30
    0

    node路由

    router.post('/',(req,res)=>{
        console.log(req.body,1);
        res.json({name:1,age:3})
    });
    

    javascript代码

    function ajax(options){
        let {url,success} = options,
            type = options.type || 'GET',
            data = options.data || null,
            contentType = options.contentType || 'application/x-www-form-urlencoded';
        let req = new XMLHttpRequest();
        req.open(type,url,true);
        if(/^POST$/i.test(type)) req.setRequestHeader('Content-Type',contentType);
        req.onreadystatechange = function(){
            console.log(req.status,req.readyState);
            if(req.readyState === 4 && req.status === 200){
                success && success(req.response);
            }
        }
        req.send(data);
    }
    
    ajax({
        url:'/aaa',
        type:'post',
        data:{"a":1,"b":2},
        success:(data)=>{
            console.log(data);
        }
    });
    
    
    

    data是 {"a":1,"b":2}
    后台输出 { 'object Object': '' } 1

    data是 "abc"
    后台输出 { abc: '' } 1

    data是

    var formData = new FormData();
    formData.append('a',1);
    

    后台输出

    { '------WebKitFormBoundaryhR4KppDAZDAMRtnj\r\nContent-Disposition: form-data; name': '"a"\r\n\r\n1\r\n------WebKitFormBoundaryhR4KppDAZDAMRtnj--\r\n' } 1
    

    这是什么问题?平常都是用的jquery的ajax传的没什么事。
    后台向前台传数据可以正常接受

    0
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 10 元积分
        全部回答
    • 共 0 条
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部