【问题标题】:I'm getting a 404 Error when I run a $http POST request with AngularJS使用 AngularJS 运行 $http POST 请求时出现 404 错误
【发布时间】:2017-06-23 22:19:03
【问题描述】:

我正在构建一个允许用户在投注池中投注的应用程序。当用户点击一个按钮时,控制器中的一个功能被执行,但没有发出请求并返回 404 错误,任何建议将不胜感激。

function placeBet(bet) {
console.log('in place bet');

$http.post('https://colossusdevtest.herokuapp.com/api/tickets.json', {
  'id': 550900,
  'status': 'PLACED',
  'customer_payout': '0.0',
  'stake': bet,
  'currency': 'GBP',
  'lines': 24,
  'amount_owned_customer': '1.0',
  'cost': '12.0',
  'created_at': '2015-07-23T17:00:10.000Z',
  'offer_amount': '0.0',
  'customer_winnings': '0.0',
  'offers_accepted': '0.0',
  'merchant_ref': '15ac9a2e-d4a8-4652-a60c-2b57d1afd92b',
  'pool': {
    'id': 171,
    'name': 'Mixed 4',
    'type_code': 'POINT_MARGIN',
    'headline_prize': '4996.06',
    'status': 'OPEN',
    'sched_start': '2015-07-28T11:00:00.000Z',
    'sched_next': '2015-07-28T11:00:00.000Z',
    'currency': 'GBP',
    'leg_num': 4,
    'sport_code': 'RUGBY',
    'has_offers': false
  }
}).then(() => {
  console.log('information posted');
});
}

$scope.placeBet = placeBet;

Problem

【问题讨论】:

  • 您确定可以进行 POST 吗? GET 工作正常
  • 您是否在提出 CORS 请求?如果是,您的 Heroku 应用是否设置为允许 CORS 请求?

标签: javascript angularjs http post http-status-code-404


【解决方案1】:

我看不到你的图片描述。

您的通话后可能出现的问题可能很少。 - 如果你有正确的 url - 你的 json 结尾听起来不太好 - 确保您有正确的数据 - 您可能需要 http.post 的配置部分。 click here - 之后添加 catch 块以捕获错误 - 检查是否是 CORS 问题

首先确保您可以使用 chrome 的 Postman 插件连接到您的服务器,然后将您的调用打包到 angular http.post 中。

【讨论】:

    【解决方案2】:

    您是否尝试将代码更改为:

    return $http({
      method: 'POST',
      url: '/someUrl',
      data: bet
    }).then(function successCallback(response) {
        console.log(response);
    }, function errorCallback(response) {
    });
    

    请确保您的请求不会跨域请求。

    【讨论】:

      猜你喜欢
      • 2017-09-09
      • 1970-01-01
      • 1970-01-01
      • 2018-05-27
      • 2016-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-12
      相关资源
      最近更新 更多