Angular2 http.get(url)正在返回404的有效网址 - 而不是CORS问题 [英] Angular2 http.get(url) is returning 404 on a valid url - not a CORS issue
问题描述
我正在运行一个正在尝试调用Django服务器的Angular2 App。
I have an Angular2 App running that is attempting to make a call to my Django server.
我有一个名为getObjects的函数,使外部API调用,并返回一个对象列表。作为一个调试,我正在做一个XMLHttpRequest,它正确地从我的服务器抓取数据。
I have a function called getObjects That makes the external API call and should return a list of objects. As a bit of debugging I am currently making a XMLHttpRequest, which correctly grabs the data from my server.
然而,每当我尝试使用Http.http提出请求。 get(url)我看到404错误。我尝试使用几个HTTP标头,以及在我的服务器上播放一些设置,但我无法弄清楚是什么。
However, whenever I attempt to make a request using Http.http.get(url) I see a 404 error. I've tried fiddling with a few of the HTTP headers, as well as playing around with some settings on my server, but I cannot figure out what is wrong.
我假设Http.http.get()方法在我不知道的幕后做一些有趣的事情。奇怪的是,我的浏览器开发网络标签甚至没有显示任何尝试到达我的外部浏览器。
I assume that the Http.http.get() method is doing something funky behind the scenes that I am unaware of. The odd thing is that my browser dev-network tab doesn't even show any attempt to reach my external browser.
任何帮助的事情将不胜感激,我可以提供任何其他调试信息,并且可以真正使用正确的方向。
Any help in the matter would be much appreciated, I can provide any additional debug information and could really use a point in the right direction.
ng --version
angular-cli: 1.0.0-beta.24
node: 6.9.1
os: win32 x64
@angular/common: 2.4.3
@angular/compiler: 2.4.3
@angular/core: 2.4.3
@angular/forms: 2.4.3
@angular/http: 2.4.3
@angular/platform-browser: 2.4.3
@angular/platform-browser-dynamic: 2.4.3
@angular/router: 3.4.3
@angular/compiler-cli: 2.4.3
import { Injectable } from '@angular/core';
import { Headers, Http } from '@angular/http';
import 'rxjs/add/operator/toPromise';
...
constructor(private http: Http) { }
getObjects(): Promise<Object[]> {
function httpGet(theUrl)
{
var xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", theUrl, false ); // false for synchronous request
xmlHttp.send( null );
return xmlHttp.responseText;
}
var x = httpGet(this.myUrl);
console.log(x); // This works perfectly and I see an entry on my server log
this.http.get(this.myUrl)
.subscribe(res => console.log(res));// This throws a 404 and I see no entry on my server logs nor in my browser network tab
var headers = new Headers({'content-type': 'application/json'});
this.http.get(this.myUrl, {headers: headers})
.subscribe(res => console.log(res));// This throws a 404 and I see no entry on my server logs nor in my browser network tab
return this.http.get(this.myUrl)
.toPromise()
.then(response => response.json().data as Object[])
.catch(this.handleError); //The original code that worked on an internal in-memory-api
}
推荐答案
我能够在另一个堆栈溢出问题中找到一个工作修复:
I was able to find a working fix in another stack overflow question:
问题是使用InMemoryWebApiModule,解决方案是对我的app.module.ts文件进行以下配置修改:
The problem was with the InMemoryWebApiModule and the solution was to make the following config modification to my app.module.ts file:
- InMemoryWebApiModule.forRoot(InMemoryDataService),
+ InMemoryWebApiModule.forRoot(InMemoryDataService, {passThruUnknownUrl: true}),
使这个小的配置更改导致应用程序工作。
Making this small config change caused the application to work.
这篇关于Angular2 http.get(url)正在返回404的有效网址 - 而不是CORS问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!