代理 ajax 请求失败,状态码 200

发布于 2024-11-15 16:37:55 字数 2532 浏览 0 评论 0原文

我正在构建一个仅 js 的 Web 应用程序,通过反向代理使用外部 API。

为此,我设置了 Apache,添加了以下规则:

ProxyRequests off
ProxyPassReverseCookiePath /starmobile /
<Location /api/>
    ProxyPass http://209.90.224.106:7500/starmobile/
    ProxyPassReverse http://209.90.224.106:7500/starmobile/
</Location>

<Proxy *>
    Order deny,allow
    Allow from all
    Satisfy all
</Proxy>

这样,所有对 /api/ 的请求都会被代理到 api 服务器,而所有其他请求则在本地处理(从而允许加载静态资源和 js)。

当使用curl通过代理执行请求时,一切工作正常:

curl -d "{\"method\":\"getAllItemClasses\"}" http://starcom/api/AvailabilityWebMobile/
[{"code":"Default","id":999},{"code":"06KALANCHOE","id":100041},{"code":"06BEGONIA","id":100043},{"code":"06ROSES","id":100045},{"code":"04AFRICANVIOLET","id":100046},{"code":"04CYCLAMEN","id":100047},{"code":"04ROSES","id":100048},{"code":"04ASST","id":100049},{"code":"04BEGONIA","id":100050},{"code":"04MUM","id":100051},{"code":"04KALANCHOE","id":100052},{"code":"04GERBERA","id":100053},{"code":"04PRIMROSE","id":100054},{"code":"06AZALEA","id":100055},{"code":"06CYCLAMEN","id":100056},{"code":"06MUM","id":100057},{"code":"06GERBERA","id":100058},{"code":"06HYDRANGEA","id":100059},{"code":"06ASST","id":100060},{"code":"06TULIP","id":100063},{"code":"06HYACINTH","id":100064},{"code":"06EASTERLILY","id":100065},{"code":"06SHAMROCK","id":100070},{"code":"045SHAMROCK","id":100071},{"code":"05DAHLIA","id":100194},{"code":"05OSTEO","id":100195},{"code":"06DUTCH BULBS","id":100263},{"code":"06WAVE","id":100304},{"code":"18PKWAVE","id":100305},{"code":"PTWAVE","id":100306},{"code":"2.6QTWAVE","id":100307},{"code":"16.5CLASSICA","id":100308},{"code":"10SQTBISCOTTO","id":100309},{"code":"10TALLBISCOTTO","id":100310},{"code":"13VALLEY","id":100311},{"code":"14COCOHB","id":100312},{"code":"14.5CONTESSA","id":100313},{"code":"PGR","id":100398},{"code":"FERTILIZER","id":100399},{"code":"POTS","id":100409},{"code":"SHUTTLE","id":100410},{"code":"TRAY","id":100411},{"code":"INSERT","id":100412},{"code":"FLAT","id":100413}]

但是,当通过ajax执行相同的请求时,我得到一个状态代码200 OK,但是一个空的正文。 Firebug 将 200 OK 消息显示为红色,作为错误,但仅此而已,而 Chrome 只是在状态上显示一条“(已取消)”消息(尽管详细信息识别出响应标头是 200)。

我完全不明白为什么我没有得到正文,因为代理应该防止诸如相同域策略之类的问题......

我附加了 Chrome 显示的响应标头:

Connection:Keep-Alive
Content-Encoding:gzip
Content-Type:application/json;charset=UTF-8
Date:Wed, 15 Jun 2011 13:03:46 GMT
Keep-Alive:timeout=15, max=100
Server:Apache-Coyote/1.1
Transfer-Encoding:chunked
Via:1.1 starcom

提前致谢

I'm building a js-only webapp consuming an external API through a Reverse Proxy.

To do this I set up my Apache adding the following rules:

ProxyRequests off
ProxyPassReverseCookiePath /starmobile /
<Location /api/>
    ProxyPass http://209.90.224.106:7500/starmobile/
    ProxyPassReverse http://209.90.224.106:7500/starmobile/
</Location>

<Proxy *>
    Order deny,allow
    Allow from all
    Satisfy all
</Proxy>

This way, all requests to /api/ are proxied to the api server, leaving all other requests to be handled locally (thus allowing to load the static resources and js).

When performing a request through the proxy with curl everything works just fine:

curl -d "{\"method\":\"getAllItemClasses\"}" http://starcom/api/AvailabilityWebMobile/
[{"code":"Default","id":999},{"code":"06KALANCHOE","id":100041},{"code":"06BEGONIA","id":100043},{"code":"06ROSES","id":100045},{"code":"04AFRICANVIOLET","id":100046},{"code":"04CYCLAMEN","id":100047},{"code":"04ROSES","id":100048},{"code":"04ASST","id":100049},{"code":"04BEGONIA","id":100050},{"code":"04MUM","id":100051},{"code":"04KALANCHOE","id":100052},{"code":"04GERBERA","id":100053},{"code":"04PRIMROSE","id":100054},{"code":"06AZALEA","id":100055},{"code":"06CYCLAMEN","id":100056},{"code":"06MUM","id":100057},{"code":"06GERBERA","id":100058},{"code":"06HYDRANGEA","id":100059},{"code":"06ASST","id":100060},{"code":"06TULIP","id":100063},{"code":"06HYACINTH","id":100064},{"code":"06EASTERLILY","id":100065},{"code":"06SHAMROCK","id":100070},{"code":"045SHAMROCK","id":100071},{"code":"05DAHLIA","id":100194},{"code":"05OSTEO","id":100195},{"code":"06DUTCH BULBS","id":100263},{"code":"06WAVE","id":100304},{"code":"18PKWAVE","id":100305},{"code":"PTWAVE","id":100306},{"code":"2.6QTWAVE","id":100307},{"code":"16.5CLASSICA","id":100308},{"code":"10SQTBISCOTTO","id":100309},{"code":"10TALLBISCOTTO","id":100310},{"code":"13VALLEY","id":100311},{"code":"14COCOHB","id":100312},{"code":"14.5CONTESSA","id":100313},{"code":"PGR","id":100398},{"code":"FERTILIZER","id":100399},{"code":"POTS","id":100409},{"code":"SHUTTLE","id":100410},{"code":"TRAY","id":100411},{"code":"INSERT","id":100412},{"code":"FLAT","id":100413}]

However when performing the same request through ajax, I get a status code 200 OK, but an empty body. Firebug displays the 200 OK message in red, as an error, but says nothing more, and Chrome simply displays a "(canceled)" message on the status (though the detail recognizes the response header was a 200).

I'm completely lost as to why I'm not given the body, since the proxy should prevent issues such as with the same domain policy....

I attach the response headers as displayed by Chrome:

Connection:Keep-Alive
Content-Encoding:gzip
Content-Type:application/json;charset=UTF-8
Date:Wed, 15 Jun 2011 13:03:46 GMT
Keep-Alive:timeout=15, max=100
Server:Apache-Coyote/1.1
Transfer-Encoding:chunked
Via:1.1 starcom

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文