【问题标题】:Node/Express not finding route for Angular http json post (404)Node/Express 找不到 Angular http json 帖子的路由 (404)
【发布时间】:2016-04-24 02:19:37
【问题描述】:

我正在开发一个新的 Angular 客户端,它应该与我的 Node/Express 服务器进行通信。我目前正在尝试开发第一步,即登录。这应该是一个 http json 帖子。事实证明,每次我从客户端执行该帖子到服务器时,Node/Express 服务器都找不到我的路径的路由。

在服务器的控制台日志中,对于我的 post json 请求,我发现以下堆栈跟踪:

OPTIONS /api/auth/facebook/mobile 404 274.092 ms - 1980

节点/Express端:

我的路线

app.post('/api/auth/facebook/mobile', authenticationHandler.handleFacebookMobileLoginRequest);

定义了 json 的正文解析器:

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

在 Angular 客户端,我这样做:

http({
             method: 'POST',
             url: 'http://192.168.1.101:3000/api/auth/facebook/mobile',
             headers: { 'Content-Type': 'application/json; charset=UTF-8;' },
             data: {fbToken: authResponse.accessToken}
      })
        .then(function (response) {
 ...

有趣的事实:

  1. 当我从 Java 应用或 Android 原生应用调用时,相同的路由可以正常工作,例如

    final JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, SERVER_URL + "/api/auth/facebook/mobile", params, jsonRequestListener, errorListener);
    
  2. 1234563此外,我在 Angular 客户端遇到了一个问题,没有分别处理答案,答案永远不会出现在 .then(... 函数中。这同样适用于其他客户端。

有人有想法、线索还是我应该打电话给 Strange 博士?

【问题讨论】:

    标签: angularjs json node.js http post


    【解决方案1】:

    可能是浏览器preflighted request CORS 问题?

    尝试在 Node.js 服务器中使用 CORS module

    var cors = require('cors')
    
    app.use(cors());
    

    【讨论】:

    • 甜耶稣地狱是的,这就是解决方案!
    • 顺便说一句。出于历史目的,我的客户端是使用 Ionic 框架构建的 Angular 客户端,在从 iOS 模拟器启动查询时遇到问题
    猜你喜欢
    • 2018-04-23
    • 2021-07-28
    • 1970-01-01
    • 2015-04-13
    • 2020-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-07
    相关资源
    最近更新 更多