问题标签 [angular-http]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
360 浏览

angularjs - 在生产中使用 $httpBackend 来抽象数据服务请求是否合适?

我的应用程序中有一个数据服务,负责为我的控制器检索信息。此信息可能来自本地存储、窗口或 ajax 请求。我面临的问题是$q承诺响应看起来不像$http响应。

$q服务在这里做得很好,但它解析为它给出的对象。我真的不希望它包装响应。我知道$httpBackend 可以做到这一点。

但是该服务在 MockE2E 库中使用,我怀疑这是它的预期用途。我不知道以后如何取消它,或者如果我在同一个请求上使用它两次会发生什么,但我可以弄清楚这些问题。我的另一个担忧是,似乎没有办法将相同的配置对象传递给 $httpBackend,就像我传递给 $http 一样。$httpBackend 只接受方法、url、body 和 headers,而 $http config 允许我指定参数。

目前,我的解决方法是自己创建类似 $http 的包装器。

但我不觉得这很优雅。有没有更好/正确的方法来做到这一点?

0 投票
1 回答
1264 浏览

angularjs - 如何根据自身配置Angular中的$http服务?

我正在尝试配置$httpAngular 的服务,以在状态码为 403 时重定向到 URL。

到目前为止没有问题,但是要重定向到的 URL 来自服务器,通过正在使用的服务$http(显然)。

这是一段代码:

此代码在 Angular 中创建循环依赖项(错误)。

有没有一种方法可以做到这一点,具有来自服务器的动态 URL,并基于此在response.status403 时将用户重定向到其中一个?

0 投票
3 回答
38148 浏览

angularjs - AngularJS 使用拦截器处理 $http 404s - 承诺未定义错误

我有一个 Angular 应用程序,我想为它处理 404 形成一个 API 端点。关键组件如下:

问题是我得到一个错误ReferenceError: promise is not defined at response。这是因为ConcernService推迟了promise?我应该如何处理?

0 投票
3 回答
15435 浏览

json - Angular $http.get 到本地主机,总是在浏览器中返回 404. 200

我无法在 Angular 1.2.13 中创建成功的获取请求。

所以我试图从我的 REST Web 服务接收一些 JSON。为了测试该服务,我从浏览器(IE9、Firefox 27、Chrome 33)访问它,在所有浏览器中都可以正常工作。

但是,上面使用 angular 的代码总是提示我错误:

*{"data":"","status":404,"config":{"transformRequest":[null],"transformResponse":[null],"method":"GET","url":, "http://localhost:8080/project/local/some/getStatus"" headers":{"Accept":"application/json, text/plain, / "}}}*

使用wireshark我检查 HTTP 请求和 HTTP 响应,从浏览器和角度调用 Web 服务都返回 200和所需的 json 对象!!然而,角度提示我 404。

当我使用 ANGULAR 从 Firefox 发出获取请求并使用 Findbugs 进行调试时,在控制台中我得到 HTTP 代码 200,但 Angular 说 404。尝试调试 Angular 的代码无济于事,看不到任何奇怪的东西。

上面的代码在 IE9 中可以正常工作!

任何建议表示赞赏。

0 投票
1 回答
1656 浏览

javascript - Angular $http 未在 $routeProvider 中解析

我正在尝试将 JSON 文件中的数据显示到网页上。我的代码

$http 似乎没有解决。这是控制台输出: 控制台输出 我是 AngularJS noob 任何帮助表示赞赏:) 我被卡住了。为什么解析的对象是 ChildScope 而不是 promise ?

0 投票
1 回答
1499 浏览

php - 对 PHP 的非阻塞 Ajax 请求

我正在使用 PHP 从远程服务器下载(大)文件,此下载是通过单击网页上的下载按钮触发的。

因此,当我单击download网页上的按钮时,会向 PHP 函数发出Ajax请求(带有 angulars )。$http该函数使用cURL.

同时,我想使用 Ajax 向我的 PHP 站点发出其他请求。Pending但只要下载正在进行,所有其他 Ajax 请求都会显示状态。

所以基本上下载会阻止对 PHP 的所有其他请求。有什么办法可以避免这种堵塞吗?

0 投票
2 回答
11499 浏览

angularjs - AngularJS $http.post

我正在尝试执行 $http.post 并获得响应,我尝试了多种方式,但无法使其正常工作。我发送的数据(userData)是一个 json 对象。

而且我的状态为 404。

谢谢

0 投票
2 回答
5792 浏览

angularjs - 如何使 $httpBackend 对 URL 查询参数的顺序不敏感?

我正在使用 Angular.js$httpBackend来测试一些包装$http调用的服务(这是在 ngMock 中,而不是在ngMockE2E 中)。

似乎对 URL 查询参数expectwhen顺序很敏感。例如,如果我这样做$httpBackend.when('POST','/apiCall?X=1&Y=2').respond(/* ... */)或,如果我在 URL 中有Y=2&X=1而不是X=1&Y=2$httpBackend.expectPOST('/apiCall?X=1&Y=2'),我会得到 URL 不匹配。

我想以这样一种方式编写我的测试,即被测试的服务可以自由地更改 URL 查询字符串参数的顺序而不会破坏测试。我无法在 $httpBackend 文档中找到任何解决此问题的方法。这样做的正确方法是什么?

0 投票
2 回答
11755 浏览

angularjs - AngularJS $http.post 服务器不允许使用方法 OPTIONS 的请求

我正在尝试对服务器进行 HTTP POST。

我必须发送的数据是一个 json 对象。

问题是角度中的 $http.post 使用选项覆盖了该方法。

我可以做这个配置

并从选项更改为 POST,但我无法将内容类型设置为“应用程序/json”,并且我收到“415 不支持的媒体类型”

谢谢

0 投票
2 回答
3936 浏览

javascript - AngularJS 1.2.14 $http POST Error Response Body Empty

UPDATE Mar 21 After more investigation it looks like this is also showing a Play framework bug an XHR limitation on error status: Play 2.2.1 SimpleResult 4xx Response Body Possible via CORS XHR? That, or the RESTfulness I seek is not possible with 401 codes.

Still an issue in both spaces since a 401 gets a 0 status code in Angular $http as below. So much for RESTfulness! All I want to do is provide a response body for 401 Unauthorized. From the W3C page on 4xx: "User agents SHOULD display any included entity to the user."

UPDATE Mar 20

Further investigation points to an issue within XMLHttpRequest itself, but please let me know if I am missing something below. I am spitting out the raw XHR object inside the xhr.onreadystatechange() callback as defined inside Angular's createHttpBackend():

angular source mod to console

When I log in with a known good email and password, everything works fine:

200 OK with response JSON

But the same XHR object for a 400 Bad Request (wrong password) has an empty response but more importantly status=0, which from Mozilla docs denotes the request was never sent (but I am seeing my API server get the request and do a DB lookup, as Postman works as above):

enter image description here

What gives? How does Postman get a response but XMLHttpRequest on latest Chrome and Firefox is confused?

I am sending a POST request using $http and getting conflicting responses in the browser (Chrome and FF), and Postman. Postman works fine and has the body, but it looks like angular is "cleaning" up some response codes to handle weirdness on Android browsers when templates are cached, but I can't pinpoint the spot in $httpBackend where the raw XHR is happening to intercept the response body--maybe that's because the XHR is blocking/canceling the response? Because security? I do not have any interceptors, either.

The "happy path" login works fine and I get matching 200 OK responses in the console/firebug, but 400 Bad Request doesn't have a response body.

CORS is configured on nginx (see http://enable-cors.org/server_nginx.html) and since the happy login success works the setup is functional.

My call:

Angular is modifying the error code so the browser and angular do not agree:

angular modifies http codes from XHR response

When I send the same request via Postman, the 400 Bad Request code is the same, but the body is also a json string, as expected.

Why is this not available via $http CORS inside the onLoginError() callback?

postman works fine