【问题标题】:AngularJs $http.post -> Error: Unexpected Request POST ; no more request expectedAngularJs $http.post -> 错误:意外请求 POST ;预计不再有请求
【发布时间】:2015-02-17 22:45:32
【问题描述】:

我在这里有一个有效的 curl 请求:

curl -u testclient:testpass http://mybackend.somedomain.com/token.php -d 'grant_type=client_credentials'

现在我尝试将其转换为我的 angularJs(离子框架)前端。 (php后端在不同的服务器上,所以这可能也与CORS有关,虽然我不知道如何)

在我的前端我尝试:

var username = 'testclient';
var password = 'testpass';
var url = 'http://mybackend.somedomain.com/token.php';
var request = $http({
        method: "post",
        url: url,
        data: {
            username: username,
            password: password
        },
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
});

这导致了一些奇怪的事情,POST 请求没有被执行,而是我收到了错误消息:

Error: Unexpected request: POST http://mybackend.somedomain.com/token.php No more request expected

我在这里做错了什么?

【问题讨论】:

  • 你会在单元测试中尝试吗?

标签: angularjs curl xmlhttprequest


【解决方案1】:

您的 Angular POST 存在错误,您的数据被编码为 JSON,而不是您在标头 application/x-www-form-urlencoded 中声明的内容。首先,您应该将代码更改为以下内容。

$http({
    method: 'POST',
    url: url,
    data: $.param({
      username: username,
      password: password
    }),
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
})

【讨论】:

    猜你喜欢
    • 2016-01-17
    • 1970-01-01
    • 1970-01-01
    • 2013-11-15
    • 1970-01-01
    • 2015-05-28
    • 2015-04-16
    • 1970-01-01
    • 2015-07-08
    相关资源
    最近更新 更多