问题描述
我在英雄教程之旅中工作,效果非常好.
我用c#制作了一个web api作为一个小后端要进一步测试.当我尝试加入所述后端时,Angular返回未找到错误404,即使下一个标签位于完全相同的道路上.
这是错误:
An error occurred Object { _body: Object, status: 404, ok: false, statusText: "Not Found", headers: Object, type: null, url: "http://localhost:58225/api/Character" }
我已经尝试登录呼叫的答案,返回"[对象对象]",但我猜这是错误消息本身.
@Injectable() export class CharacterService{ private charactersUrl = 'http://localhost:58225/api/Character'; constructor(private http:Http){} getCharacters(): Promise<Character[]>{ alert("Getting Characters"); var my_data = this.http.get(this.charactersUrl) .toPromise() .then(response => response.json().data) console.log("Resultat de l'appel = "+my_data); return this.http.get(this.charactersUrl) .toPromise() .then( (response => response.json().data as Character[]) ) .catch(this.handleError); }
你可以看到这里没有什么真正的原创.
/api/character返回包含我字符列表的IEnumerable.
public IEnumerable<string> GetAll() { List<Character> myCharacs = repo.Get().ToList(); List<String> myJsonCharacs = new List<string>(); foreach (var charac in myCharacs) { var cur = jsonConverter.Serialize(charac); myJsonCharacs.Add(cur); } return myJsonCharacs; }
我真的不知道我应该如何进一步测试?我正在阅读承诺和观察到,看看我是否错过了某些东西,但这是我可以想到的最简单的例子:/
编辑:
我通过以下方式更改了API的响应:如何使用chrome返回json而不是xml的asp.net web api?.
API现在将直接浏览到IT时返回以下内容:
["{\"characterName\":\"Azazel\",\"playerName\":\"Mulhouse\",\"game\":{\"name\":\"Call of Cthulhu\",\"BaseAttributes\":[{\"max\":18,\"value\":12,\"name\":\"appearance\"},{\"max\":18,\"value\":21,\"name\":\"constitution\"},{\"max\":18,\"value\":19,\"name\":\"strength\"},{\"max\":18,\"value\":17,\"name\":\"dexterity\"},{\"max\":18,\"value\":15,\"name\":\"power\"},{\"max\":18,\"value\":13,\"name\":\"size\"},{\"max\":18,\"value\":13,\"name\":\"intelligence\"},{\"max\":24,\"value\":12,\"name\":\"education\"}],.....
没有区别. (这是一个答案的样本,它实际上更长了)
还要,由于答案是路径的404,我怀疑问题与答案的格式无关,但更多到角配置.我怎么检查这个?编辑:可能对我的对象结构之间的区别导致404错误吗?如果来自服务器的JSON字符具有比Angular2中定义的JSON字符更多的字段,会发生什么?
谢谢
推荐答案
- www是区分大小写的!
private charactersUrl = 'http://localhost:58225/api/character';
-
您的服务器应该只是响应JSON字符串
-
似乎没有data属性,所以使用它:
.then( (response => response.json() as Character[]) )
- 更改您的服务器功能(序列化为您自己!)
public string GetAll() { List<Character> myCharacs = repo.Get().ToArray(); return jsonConverter.Serialize(myCharacs); }
- 或让框架完成工作..
public IEnumerable<Character> GetAll() { List<Character> myCharacs = repo.Get().ToArray(); return myCharacs; }
其他推荐答案
朋友终于找到了答案:
删除内存数据服务引用.那东西阻止了HTTP请求.
我知道有一个不同的错误,但至少我可以到达API.
问题描述
I've worked on the Tour of Heroes tutorial,works perfectly well.
I've made a Web api with C# as a small backend to test further. When I try to join said back-end, angular returns an error 404 not found, even when next tab is on the exact same road.
This is the error :
An error occurred Object { _body: Object, status: 404, ok: false, statusText: "Not Found", headers: Object, type: null, url: "http://localhost:58225/api/Character" }
I've tried login the answer of the call, I return an "[object Object]" , but I guess that's the error message itself.
@Injectable() export class CharacterService{ private charactersUrl = 'http://localhost:58225/api/Character'; constructor(private http:Http){} getCharacters(): Promise<Character[]>{ alert("Getting Characters"); var my_data = this.http.get(this.charactersUrl) .toPromise() .then(response => response.json().data) console.log("Resultat de l'appel = "+my_data); return this.http.get(this.charactersUrl) .toPromise() .then( (response => response.json().data as Character[]) ) .catch(this.handleError); }
As you can see there's nothing really original here.
The /api/Character returns an IEnumerable that contains the list of my characters.
public IEnumerable<string> GetAll() { List<Character> myCharacs = repo.Get().ToList(); List<String> myJsonCharacs = new List<string>(); foreach (var charac in myCharacs) { var cur = jsonConverter.Serialize(charac); myJsonCharacs.Add(cur); } return myJsonCharacs; }
I don't really know how I'm supposed to test further ? I'm reading on promises and observables to see if I missed something, but this is the simplest example I could think of :/
Edit :
I have changed the response of the API for json by following this : How do I get ASP.NET Web API to return JSON instead of XML using Chrome?.
The API now returns this when I browse directly to it :
["{\"characterName\":\"Azazel\",\"playerName\":\"Mulhouse\",\"game\":{\"name\":\"Call of Cthulhu\",\"BaseAttributes\":[{\"max\":18,\"value\":12,\"name\":\"appearance\"},{\"max\":18,\"value\":21,\"name\":\"constitution\"},{\"max\":18,\"value\":19,\"name\":\"strength\"},{\"max\":18,\"value\":17,\"name\":\"dexterity\"},{\"max\":18,\"value\":15,\"name\":\"power\"},{\"max\":18,\"value\":13,\"name\":\"size\"},{\"max\":18,\"value\":13,\"name\":\"intelligence\"},{\"max\":24,\"value\":12,\"name\":\"education\"}],.....
Made no difference. (This is a sample of the answer, it's actually way longer)
Also, since the answer is a 404 to the path, I suspect the problem is not related to the format of the answer but more to angular's configuration. How could I check this?
EDIT : Could a difference between the structure of my objects cause this 404 error? If my json character coming from the server has more fields than the json character defined in angular2, what happens?
Thank you
推荐答案
- The WWW is case-sensitive!
private charactersUrl = 'http://localhost:58225/api/character';
your server should response just a JSON-string
seems like there is no data property, so use it like this:
.then( (response => response.json() as Character[]) )
- change your server function like this (serialize to json yourself!)
public string GetAll() { List<Character> myCharacs = repo.Get().ToArray(); return jsonConverter.Serialize(myCharacs); }
- or let the framework do the work..
public IEnumerable<Character> GetAll() { List<Character> myCharacs = repo.Get().ToArray(); return myCharacs; }
其他推荐答案
A friend finally found the answer :
REMOVE THE IN-MEMORY-DATA-SERVICE references. That thing blocks http requests.
I know have a different error, but at least I can reach the API.