我有这个简单的代码:
$http.get("/api/test") .success(function (data,status,headers,config) { console.log(data); return data; }).error(function (data,config) { alert("error"); return status; });
它工作正常,但错误功能从未被调用,即使我从服务器返回404(未找到)…
在这种情况下,它会调用status = 404的’success’功能
那是对的吗?
谢谢
提琴手:
Request GET http://localhost:41234/api/test HTTP/1.1 Host: localhost:41234 Connection: keep-alive Accept: application/json,text/plain,*/* X-Requested-With: XMLHttpRequest User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.22 (KHTML,like Gecko) Chrome/25.0.1364.172 Safari/537.22 Referer: http://localhost:41234/ Accept-Encoding: gzip,deflate,sdch Accept-Language: pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4 Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3 Cookie: ASP.NET_SessionId=bd1b3rib5j4beub0xbuhb1hm; FormsAuthentication=xxxxx Response HTTP/1.1 404 Not Found Cache-Control: no-cache Pragma: no-cache Expires: -1 Server: Microsoft-IIS/8.0 X-AspNet-Version: 4.0.30319 X-SourceFiles: =?UTF-8?B?RDpcUGVzc29hxvY2FyLkFwaVxhcGcg==?= X-Powered-By: ASP.NET Content-Length: 0
问题是您的网络服务器,它将content-length设置为0,这意味着您可以在
HTTP/1.1 specification看到有效的值。
另外我举了JSFiddle的例子,显示错误和成功的例子。见here。
请求:
GET /error/ HTTP/1.1 Host: fiddle.jshell.net Connection: keep-alive Accept: application/json,*/* User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.31 (KHTML,like Gecko) Chrome/26.0.1410.65 Safari/537.31 DNT: 1 Referer: http://fiddle.jshell.net/danielcsgomes/cAMc6/1/show/ Accept-Encoding: gzip,sdch Accept-Language: en-US,en;q=0.8 Accept-Charset: UTF-8,*;q=0.5 Cookie: csrftoken=4tNVNxC5v6BSq9yJCKkHlGFJBz3cClqd`
响应:
HTTP/1.1 404 NOT FOUND Server: Nginx/0.8.54 Date: Fri,12 Apr 2013 00:38:07 GMT Content-Type: text/html; charset=utf-8 Transfer-Encoding: chunked Connection: keep-alive Vary: Cookie Content-Encoding: gzip