在与Angular 2中的restful后端连接时,拒绝访问被拒绝

时间:2016-09-13 00:52:32

标签: angular

我收到以下错误:

  

“错误:访问被拒绝。\ r \ n \ n在匿名函数(eval代码:1343:17)\ n,Observable.prototype.subscribe(eval代码:52:9)\ n在Observable.prototype中。 _subscribe(eval代码:109:9)\ n在MapOperator.prototype.call(eval代码:54:9)\ n at Observable.prototype.subscribe(eval代码:52:9)\ n在Observable.prototype._subscribe(n eval代码:109:9)\ n在CatchOperator.prototype.call(eval代码:29:9)\ n在Observable.prototype.subscribe(eval代码:52:9)\ n在SearchCustomerComponent.prototype.onSearch(eval代码) :21:9)\ n at _View_SearchCustomerComponent0.prototype._handle_ngSubmit_2_0(功能代码:232:3)“

这是我的代码:

// searchCustomer.component.ts 
import { Component, OnInit } from '@angular/core';
import { NgForm } from '@angular/forms';
import { CustomerAcct } from '../models/customer-acct';
import { CustomerService } from '../services/customer.service';

@Component ({
    selector: 'search-customer',
    templateUrl: './app/customer/SearchCustomer.component.html'

})

export class SearchCustomerComponent {

    errorMessage: string;
    ca: CustomerAcct;
    mode = 'Observable';
    custAcctNo: number;

    constructor (private customerService: CustomerService) {}

    onSearch(scf: NgForm){

        console.log("custAcctNo = ", this.custAcctNo);
        this.customerService.getCustomer(this.custAcctNo).subscribe(
            data => this.ca = data,
            error => this.errorMessage = <any>error);
        console.log("this.ca ", this.ca.customerAcctId);
    }

}




// customer.service.ts
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';

import { Observable } from 'rxjs/Observable';
import { CustomerAcct } from '../models/customer-acct';

@Injectable()
export class CustomerService {

    constructor (private http: Http) {}

    private backendURL = 'localhost:8080/BAG';

    getCustomer(custAcct: number): Observable<CustomerAcct> {
        // let custURL = this.backendURL.concat('customer/findCustomer');
        let custURL = 'localhost:8080/BAG/customer/findCustomer';
        return this.http.get(custURL)
                        .map(this.extractData)
                        .catch(this.handleError);
    }

    private extractData(res: Response) {
        // Response object holds data into JSON object, so need to parse it using res.json() method.
        let body = res.json();
        return body.data || { };
    }

    private handleError(error: any) {
        // TO DO use a remort logging infrastructure 
        let errMsg = (error.message) ? error.message : 
            error.status ? `${error.status} - ${error.statusText}` : 'Server error';
            console.error(errMsg); 
        return Observable.throw(errMsg);
    }
}
<!-- SerachCustomer.component.html -->
<div>
        <form (ngSubmit)="onSearch(searchCustomer)" #searchCustomer="ngForm">
                <div class="col-lg-3">
                        <div class="input-group">
                                <input type="text" 
                                        class="form-control"
                                        id="custAcctNo" 
                                        placeholder="Search Customer Acct..." 
                                        [(ngModel)]="this.custAcctNo"
                                        name="custAcctNo" required>
                                <span class="input-group-btn">
                                        <button class="btn btn-default" type="submit">Search</button>
                                </span>
                        </div><!-- /input-group -->
                </div><!-- /.col-lg-6 -->
        </form>
</div>

0 个答案:

没有答案