doupo6967
2017-08-06 20:03
浏览 38
已采纳

Angular $ http post call将数据发布传递到GO后端

I am trying to get access to backend written in GO which in 99% is good (problem does not lay there).

For now I just created simplest call which stay in controller (it will be in service in future) to register new user. Although I hardcoded data which I am passing the response says 403 forbidden. In powerShell shows reason of 403:

RegistrationForm parse - email: , nick:

Validation failed for email - blank

It looks like I am not passing my data correctly because email is blank. Please take a look at my code:

$ctrl.fake_registerSubmit = function() {

    $http({
        url: 'http://localhost:3000/v1/sign_up',
        headers: {
           'Content-Type': 'application/x-www-form-urlencoded'
         },
        method: 'POST',
        data: {
            email: 'check@onet.pl',
            nick: 'borysxoxo',
            password: 'admin12312',
            password_confirmation: 'admin12312'
        }
    })

    .then(function successCall(response, post) {
        console.log('user added');

    }, function errorCall(respone) {
        console.log('error callback');
        console.log(respone);
    })

};

This screenshot presents API documentation which I am trying to access:

link

What am I doing wrong? Is there other way to pass data?

图片转代码服务由CSDN问答提供 功能建议

我正在尝试访问以GO编写的后端,该后端在99%内是不错的(问题不在那儿) 。

现在,我只创建了最简单的调用,该调用保留在控制器中(以后将投入使用)以注册新用户。 尽管我对通过的数据进行了硬编码,但禁止使用403。 在powerShell中显示403的原因:

RegistrationForm解析-email:,昵称:

电子邮件验证失败-空白

由于电子邮件为空白,看来我没有正确传递数据。 请看一下我的代码:

  $ ctrl.fake_registerSubmit = function(){
 
 $ http({
 url:'http:// localhost:  3000 / v1 / sign_up',
标头:{
'Content-Type':'application / x-www-form-urlencoded'
},
方法:'POST',
数据:{
 电子邮件:'check@onet.pl',
昵称:'borysxoxo',
密码:'admin12312',
 password_confirmation:'admin12312'
} 
})
 
 .then(function successCall( 响应,发布){
 console.log('添加了用户'); 
 
},函数errorCall(respone){
 console.log('错误回调'); 
 console.log(respone);  
})
 
}; 
   
 
 

此屏幕快照显示了我要访问的API文档:

链接

我在做什么错了? 还有其他方法可以传递数据吗?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doufu8887 2017-08-06 20:29
    已采纳

    You are sending some json data with the wrong Content-Type. I see 2 options, either change Content-Type in your header to:

    'Content-type' : 'application/json'

    or transform your payload to:

    data: "email=check@onet.pl&nick=borysxoxo&password=admin12312&password_confirmation=admin12312"
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题