Angular $http post调用传递数据问题到GO后端

huangapple go评论102阅读模式
英文:

Angular $http post call passing data issue to GO backend

问题

我正在尝试访问用GO编写的后端,99%的情况下是没有问题的(问题不在那里)。

目前,我只创建了一个最简单的调用,它留在控制器中(将来会在服务中),用于注册新用户。尽管我硬编码了要传递的数据,但响应显示为403禁止访问。在PowerShell中显示403的原因是:

> RegistrationForm parse - email: , nick:
>
> 邮件验证失败 - 邮件为空

看起来我没有正确传递我的数据,因为邮件为空。请看一下我的代码:

$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);
    })

};

这个屏幕截图展示了我正在尝试访问的API文档:

链接

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

英文:

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?

答案1

得分: 0

你正在使用错误的Content-Type发送一些JSON数据。
我看到有两个选项,要么在你的头部中将Content-Type更改为:

'Content-type': 'application/json'

要么将你的有效载荷转换为:

data: "email=check@onet.pl&nick=borysxoxo&password=admin12312&password_confirmation=admin12312"
英文:

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"

huangapple
  • 本文由 发表于 2017年8月7日 04:03:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/45536116.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定