[英]Unable to get data from node js service to angular js 2
我正在学习 Angular,我正在尝试借助 angular 2 中的服务从节点 js 服务中获取数据。
当我从浏览器执行 node js 服务时,我能够看到结果,但是当我尝试以 angular 获取数据时,它会引发错误。
在这里,我在 node js 和 angular 中创建了服务,我正在尝试获取数据,但它引发了错误。
节点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");
});
}
}
错误
Response headers : Headers {_headers: Map(0), _normalizedNames: Map(0)} ok : false status : 0 statusText : "" type : 3 url : null
_body : ProgressEvent {isTrusted: true, lengthComputable: false, loaded: 0, total: 0, type: "error", …} proto : Body
以防万一其他人遇到这个问题:当您在不同的端口/域上运行前端和后端时(在这个问题中,我假设您使用的是 angular-cli,因此默认域是http://localhost :4200 ) 并且服务器在http://localhost:5000 中运行,您应该启用 CORS 才能访问服务器。 如果您使用 express(作为 OP),请检查此链接以在您的节点服务器中启用 cors: https ://enable-cors.org/server_expressjs.html
亲切的问候。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.