Javascript 客户端无法访问其自己的服务器终结点

Javascript 客户端无法访问其自己的服务器终结点,javascript,angularjs,node.js,express,angular-fullstack,Javascript,Angularjs,Node.js,Express,Angular Fullstack,我有一个AngularFullStack项目,在该项目中,我试图使用以下代码访问我的项目的服务器端点,但是问题是调用没有从angular客户端传递到nodejs服务器。因为这是我的第一个项目,所以我几乎不知道是什么地方出了问题,导致了这场奇怪的冲突 客户端: $http({ method: 'POST', url: '/api/soapAPIs/soapAPI', data: request, headers: {

我有一个AngularFullStack项目,在该项目中,我试图使用以下代码访问我的项目的服务器端点,但是问题是调用没有从angular客户端传递到nodejs服务器。因为这是我的第一个项目,所以我几乎不知道是什么地方出了问题,导致了这场奇怪的冲突

客户端:

$http({
        method: 'POST',
        url: '/api/soapAPIs/soapAPI',
        data: request,
        headers: {
          'Content-Type': 'application/json'
        }
      })
      .then(function(response) {
        console.log("response from server is: ", response);
      })
      .catch(function(error) {
        console.log("error in calling server is: ", error);
      });
export function soapAPI(req, res) {
  console.log("SERVER SIDE ", req.body);
  res.status(statusCode).json({"status":"success"});
  res.send();
}
我已经在服务器端安装了CORS,并在app.js中编写了以下代码,但仍然不起作用

服务器App.js

// Setup server
var app = express();
var cors = require('cors');
//add cors to do the cross site requests
app.use(cors());
服务器端点代码:

$http({
        method: 'POST',
        url: '/api/soapAPIs/soapAPI',
        data: request,
        headers: {
          'Content-Type': 'application/json'
        }
      })
      .then(function(response) {
        console.log("response from server is: ", response);
      })
      .catch(function(error) {
        console.log("error in calling server is: ", error);
      });
export function soapAPI(req, res) {
  console.log("SERVER SIDE ", req.body);
  res.status(statusCode).json({"status":"success"});
  res.send();
}
以下是问题:

  • 如果我尝试像这样点击nodejs端点
    /api/soapAPI/soapAPI
    ,浏览器将显示
    pending
    请求,并且它永远不会进入服务器

  • 当我为端点添加完整的分类url时,如http:localhost:9000/api/soapAPI/soapAPI,它会命中端点,但客户端从未收到来自服务器的响应。由于明显的原因,我不想在客户端提供完整的分类URL

  • 我如何解决这个问题?如果您需要任何其他代码/信息,请告诉我

    编辑:

    $http({
            method: 'POST',
            url: '/api/soapAPIs/soapAPI',
            data: request,
            headers: {
              'Content-Type': 'application/json'
            }
          })
          .then(function(response) {
            console.log("response from server is: ", response);
          })
          .catch(function(error) {
            console.log("error in calling server is: ", error);
          });
    
    export function soapAPI(req, res) {
      console.log("SERVER SIDE ", req.body);
      res.status(statusCode).json({"status":"success"});
      res.send();
    }
    
    通过使用完全分类的路径,服务器端点会被命中,但客户端永远不会收到其响应。我在浏览器中遇到此错误:

    Possibly unhandled rejection: {"data":null,"status":-1,"config":{"method":"GET","transformRequest":[null],"transformResponse":[null],"jsonpCallbackParam":"callback","url":"/api/things","headers":{"Accept":"application/json, text/plain, */*"}},"statusText":""}
    

    您必须使用
    res.send()


    取消对以下行的注释:

    /*mongoose.connect(config.mongo.uri, config.mongo.options);
      mongoose.connection.on('error', function(err) {
      console.error(`MongoDB connection error: ${err}`);
      process.exit(-1); // eslint-disable-line no-process-exit
    });
    */
    

    基本上,这会导致控制器不返回任何响应,因为您的mongoose模型正在被引用,但与mongo的连接失败。

    .json()方法也会发送响应。你不必使用.send()/@BadisMerabet,我试过使用res.send()和不使用res.send()都不起作用。你有没有注意到这里有两个大括号:res.status(statusCode).json({“status”:“success”});另外,请尝试从(状态)中删除双引号(“)。是的,这只是一个输入错误。我的代码中没有错误。请共享server.js(gist,github,…)@BadisMerabet的完整代码。现在开始: