2017-08-31 86 views
0

我学习角,我试图与服务的角度2.无法从节点JS业务数据的角度JS 2

的帮助,以便让一个节点js的服务数据,当我执行节点的js来自浏览器的服务我能够看到结果,但是当我试图以角度获取数据时会引发错误。

这里我已经创建了服务节点js和从角度我想获取数据,但它会引发错误。

节点JS:

 var http = require("http"); 

     var express = require('express'); 
     var app = express(); 
     var sql = require('mssql'); 
     var data; 
     var config = { 
      server: 'INBGMW-C037', 
      database: 'InvoiceReminders', 
      user: 'sa', 
      password: 'psi' 
     }; 
     app.get('/serviceline_dtl', function (req, res) { 
      // connect to your database 
      sql.close(); 
      sql.connect(config, function (err) { 

       if (err) console.log(err); 
       // create Request object 
       var request = new sql.Request(); 
       request.input('p_Flag', sql.VarChar, "ALL_SERVICE_LINE") 
       request.output('po_Retval',sql.Int) 
       request.output('po_UpdatedBy',sql.VarChar) 
       request.output('po_UpdatedTime',sql.DateTime) 
       request.output('po_Message',sql.VarChar) 
       // query to the database and get the records 
       request.execute("[dbo].[ARA_SP_DS_GetAllSLDetails]").then(function(recordSet) { 
        if (recordSet == null || recordSet.length === 0) 
         return; 

        // res.send(recordset); 
        data=recordSet.recordsets; 
        res.send(JSON.stringify(data)); 
        console.log(data); 
        sql.close(); 
       }).catch(function (err) {   
        console.log(err); 
        sql.close(); 
       }); 
      }); 

     }); 

     var server = app.listen(5000, function() { 
      console.log('Server is running..'); 
     }); 

 import { Injectable } from '@angular/core'; 

     import { Http, Response, Headers, RequestOptions } from '@angular/http'; 

     import { serviceLine } from './models/serviceLine'; 

     import {Observable} from 'rxjs/Rx'; 

     // Import RxJs required methods 
     import 'rxjs/add/operator/map'; 
     import 'rxjs/add/operator/catch'; 


     @Injectable() 
     export class HttpService { 

      private BASE_URL:string = 'http://localhost:5000/serviceline_dtl'; 

      constructor(
        private http: Http 
      ) { } 

      public getServiceLinedtl(){ 

       return this.http.get(`${this.BASE_URL}`) 
        .map((res:Response) => res.json()); 
        //.catch((error:any) => Observable.throw(error.json().error || 'Server error')); 
      } 

     } 

组分:

import {Component,OnInit ,OnChanges} from '@angular/core'; 
    import {IMyDpOptions} from 'mydatepicker'; 
    import {HttpService} from './http.service'; 

    @Component({ 
     selector: 'my-app', 
     styleUrls:['/css/home.css'], 
     templateUrl:'./SpotCheckStatus.html', 
     providers:[HttpService] 

    }) 
    export class SpotCheckStatusComponent implements OnInit 
    { 

     constructor(
     private httpService: HttpService 
    ) {} 

     ngOnInit(){ 

     this.httpService.getServiceLinedtl().subscribe(
         response=> { 
          console.log("VALUE RECEIVED: ",response); 
          }, 
         error=> { 
          console.log("ERROR: ",error); 
         }, 
         () => { 
          console.log("Completed"); 
         } 

        ); 
    } 
    } 

错误

响应头:头{_headers:地图(0),_normalizedNames: 地图(0)}确定:假状态:0状态文本: “” 型:3网址:空 _body:ProgressEvent {isTrusted:真,lengthComputable :false,loaded:0,total:0,type:“error”,...} proto:Body

+1

什么错误提示的抛出 –

+0

响应 头 : 头{_headers:地图(0),_normalizedNames:地图(0)} OK : 假 状态 :状态文本 : “” 类型 :网址 : 空 _body : ProgressEvent {isTrusted:真,lengthComputable :false,loaded:0,total:0,type:“error”,...} __proto__ : Body – user3301440

+0

以上是我得到的回应 – user3301440

回答

1

为了防止别人提出这个问题:当前端和后端运行时不同的端口/域(在这个问题中,我假设你正在使用angular-cli,所以默认域为http://localhost:4200),服务器运行在http://localhost:5000,你应该启用CORS才能够到达服务器。如果您使用express(作为OP),请检查以下链接以在您的节点服务器中启用cors:https://enable-cors.org/server_expressjs.html

此致敬意。