简体   繁体   English

Angular 4和ASP.Net MVC 5:作为响应返回一个空JSON

[英]Angular 4 and ASP.Net MVC 5 : returns an Empty JSON in response

after merging angular app with asp.net MVC calling API from angular returns an empty JSON. 将angular app与asp.net MVC合并,从angular调用API后,返回空JSON。

The angular and asp.net are in the same domain. angular和asp.net在同一个域中。

If I call the API With PostMan, I have a JSON with the result. 如果我使用PostMan调用API,则结果中将包含一个JSON。 but if I call it in the angular app my JSON result is empty. 但是如果我在angular应用程序中调用它,我的JSON结果为空。

Are there any tips for communicating angular app with asp.net MVC after merging and serving in the same domain? 在同一域中合并并提供服务后,是否有任何技巧可与asp.net MVC通信angular应用程序?

Update 1: 更新1:

The code that used to calling Webservice: 用来调用Webservice的代码:

 getSheets(): Observable<Sheet[]> {
return this.http.get(this.config.apiUrl + '/api/SheetsRelationAPI', 
this.jwt())
  .map(this.extractData)
  .do(data => console.log('SheetsData:', data))  // debug
  .catch(this.handleError);
 }


/**
* Handle HTTP error
*/
 private handleError(error: any) {
// In a real world app, we might use a remote logging infrastructure
// We'd also dig deeper into the error to get a better message
const errMsg = (error.message) ? error.message :
  error.status ? `${error.status} - ${error.statusText}` : 'Server error';
console.error(errMsg); // log to console instead
return Observable.throw(errMsg);
  }

// private helper methods

 private jwt() {
// create authorization header with jwt token
const currentUser = JSON.parse(atob(this.cookie.getCookie('currentUser')));
  if (currentUser && currentUser.access_token) {
  const headers = new Headers({ 'Authorization': 'Bearer ' + currentUser.access_token},
  );
  return new RequestOptions({ headers: headers });
  }
  }
   private extractData(res: Response) {
const body = res.json();
return body || [];
 }

Update 2: 更新2:

I notice that my API if I called it from outside domain it respond 2 times: 我注意到,如果我从外部域调用我的API,它会响应2次:

inspecting network with google chrome inspect element: 用google chrome检查网络检查元素:

the first response is "zone.js" initiator and the second response is an "other" initiator 第一个响应是“ zone.js”启动器,第二个响应是“其他”启动器

If I call the API from inside of the Domain I just have a response from "zone.js" initiator and it returns an empty JSON. 如果我从域内部调用该API,则只会收到“ zone.js”启动器的响应,并且它将返回一个空的JSON。

Update 3 更新3

export class OtherComponent implements OnInit {
sheets: Sheet[] = [];
errorMessage: string;
constructor(private httpService: HttpService) {
    // this.sheets = this.ichartHttp.getSheets();
    // console.log(this.sheets);
}
getSheets() {
    this.httpService.getSheets()
        .subscribe(
        sheets => this.sheets = sheets,
        error => this.errorMessage = <any>error
        );
}

ngOnInit() {
    this.getSheets();
}
}

The Problem is with my Authentication methods, I use two types of authentication, MVC and WebAPI they conflict if I send a request to API under the same Domain. 问题出在我的身份验证方法上,我使用两种类型的身份验证,即MVC和WebAPI,如果我向同一域下的API发送请求,它们会冲突。

So my Answer is: Your Angular Code looks good, take a look at your middleware project 所以我的答案是:您的Angular代码看起来不错,请看一下您的中间件项目

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM