localhost REST API请求错误ionic2 angular2

时间:2016-07-06 04:02:34

标签: node.js rest express angular ionic2

我在Ionic 2应用程序中向本地托管的REST API服务器发出get / post请求。下面的错误显示了几秒钟。

  

3 387557 group EXCEPTION:响应状态:0表示URL:null

     

4 387558错误EXCEPTION:状态响应:URL为0:null

     

5 387558 groupEnd

     

6 387568错误未捕获响应状态:0表示URL:null,http://localhost:8100/build/js/app.bundle.js,行:88826

我能够向本地服务器发出成功的curl请求。这是我的代码供参考。

app.js

var express = require("express");
var mysql   = require("mysql");
var bodyParser  = require("body-parser");
var SHA256 = require("sha256");
var rest = require("./REST.js");
var app  = express();

function REST(){
  var self = this;
  self.connectMysql();
};

REST.prototype.connectMysql = function() {
  var self = this;
  var pool      =    mysql.createPool({
    connectionLimit : 100,
    host     : 'host',
    user     : 'user',
    password : 'password',
    database : 'database',
    debug    : false
  });
  pool.getConnection(function(err,connection){
    if(err) {
      self.stop(err);
    } else {
      self.configureExpress(connection);
    }
  });
}

REST.prototype.configureExpress = function(connection) {
  var self = this;
  app.use(bodyParser.urlencoded({ extended: true }));
  app.use(bodyParser.json());
  var router = express.Router();
  app.use('/api', router);
  var rest_router = new rest(router,connection,SHA256);
  self.startServer();
}

REST.prototype.startServer = function() {
  app.listen(3000, function() {
    console.log("All right ! I am alive at Port 3000. OKAY BUDDY");
  });
}

REST.prototype.stop = function(err) {
  console.log("ISSUE WITH MYSQL n" + err);
  process.exit(1);
}

new REST();

REST.js

var mysql = require("mysql");

function REST_ROUTER(router, connection, SHA256) {
  var self = this;
  self.handleRoutes(router, connection, SHA256);
}

REST_ROUTER.prototype.handleRoutes= function(router,connection,SHA256) {
  router.get("/",function(req,res){
    res.json({'foo': 'bar'});
  });
});

login.js(组件)

import {Component} from '@angular/core';
import {NavController} from 'ionic-angular';
import {AuthProvider} from '../../providers/auth/auth';

/*
  Generated class for the LoginPage page.

  See http://ionicframework.com/docs/v2/components/#navigation for more info on
  Ionic pages and navigation.
*/
@Component({
  templateUrl: 'build/pages/login/login.html',
  providers: [AuthProvider]
})
export class LoginPage {
  static get parameters() {
    return [[NavController], [AuthProvider]];
  }

  constructor(nav, AuthProvider) {
    this.nav = nav;
    this.authProvider = AuthProvider;
    this.form = {};
  }

  login(form) {
    this.authProvider.login(form).then(res => {
      alert(JSON.stringify(res));
    });
  }
}

auth.js(提供者)

import {Injectable} from '@angular/core';
import {Http, Headers, RequestOptions} from '@angular/http';
import 'rxjs/add/operator/map';

/*
  Generated class for the Auth provider.

  See https://angular.io/docs/ts/latest/guide/dependency-injection.html
  for more info on providers and Angular 2 DI.
*/
@Injectable()
export class AuthProvider {
  static get parameters(){
    return [[Http]]
  }

  constructor(http) {
    this.url = 'http://localhost:3000/api';
    this.http = http;
  }

  login(form) {
    return new Promise(resolve => {
      this.http.get(this.getUrl)
        .map(res => res.json())
        .subscribe(data => {
          resolve(data);
        });
    });
  }
}

2 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,并且能够解决它。我在localhost:8000上提供API。当ionic向localhost或127.0.0.1发出请求时,我认为它已被阻止。我找到了我的计算机的IP地址并在0.0.0.0:8000上托管了我的网络服务器,而不是点击http://localhost:8000/api/my/endpoint我点击http://mycomputerip:8000/api/my/endpoint,它有效!

答案 1 :(得分:0)

您正尝试在auth.js login函数中请求输入错误的空网址:

this.http.get(this.getUrl)

您的代码示例中未定义this.getUrl。轻松修复:

this.http.get(this.url)