【问题标题】:how to send json format to server如何将json格式发送到服务器
【发布时间】:2016-09-13 20:23:54
【问题描述】:

我希望数据采用这种格式

{"email":"s@gmail.com","password":"1"}

但是采用这种格式

{ '{"email":"s@gmail.com","password":"1"}': '' }

我的客户代码,

headers.append('Content-Type', 'application/x-www-form-urlencoded')
        this.http.post(this.header1+'login', JSON.stringify(form), { headers: headers })

这个我不太清楚,有大神帮忙看看

我的后端代码,

exports.login = function( req, res ) {

console.log(req.body)
  var query = 'select * from profile where email = ? and password = ?'; 
 connection.query(query,[req.body.email,req.body.password],function(error,result,rows,fields){
    if(!!error){console.log(error)
      console.log('fail');
    }else{
      console.log(result);
      res.send(result);
    }
  // }

  });}

【问题讨论】:

  • 试试this.http.post(this.header1+'login', form, { headers: headers })
  • 不要追加内容类型,也不要使用 JSON.stringify。
  • 您的 form 已经是 JSON。如果你字符串化,结果是一个string 对象。
  • 所以我应该删除 stringify?
  • 删除后也没用

标签: javascript angularjs node.js express angular


【解决方案1】:

试试这个

   $.ajax({
       url: this.header1+'login',
       data: {"email":"s@gmail.com","password":"1"},
       type: 'POST',
       success: function(response) {
          alert(response);
       }
    });

【讨论】:

    【解决方案2】:

    如果你想使用 jquery 这很简单:

    $.post('http://address', {"email":"s@gmail.com","password":"1"}).done( (response) => {
      // Request complete
    }).fail((jqXHR) => {
      // Request failed 
    });
    

    【讨论】:

    • 我正在使用 angular2
    • 那可能是你的后端有问题
    【解决方案3】:
    Try This
    
        $http({
        url: this.header1 + 'login',
        ,
        method: "POST",
        data: {
            "email": "s@gmail.com",
            "password": "1"
        },
        headers: {
            'Content-Type': 'application/json;',
            'Access-Control-Allow-Origin': '*'
    
        }
    }).success(function (response) {
    
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-06
      • 1970-01-01
      • 1970-01-01
      • 2013-12-27
      • 1970-01-01
      • 1970-01-01
      • 2011-03-11
      • 2016-01-16
      相关资源
      最近更新 更多