2017-08-31 85 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

此致敬意。