douyong1850 2014-09-04 10:42
浏览 217
已采纳

服务器拒绝一个请求但接受另一个请求。 为什么?

I am making two HTTP GET requests to my (server) webservice in Java from a PHP website I am developing currently. I use the same function for making the GET requests. Server rejects one request & returns null as response while it accepts the other one & returns the correct response. Both these GET requests when tried from Google Chrome's extension POSTMAN returns correct response. Why is server acting so weird like this?? For the request that server rejected, it says No 'Access-Control-Allow-Origin' header is present on the requested resource. Why is it so??? So, this means for the request that the server accepted, this particular header is present. I am confused.

  • 写回答

1条回答 默认 最新

  • dougu1896 2014-09-04 11:16
    关注

    The Access-Control-Allow-Origin is some kind of security-header sent by the server to prevent your server being called by random other servers but all processing of these headers is left to the client side. This means that your client-library (your webbrowser for example) checks if the domain currently visited is present in the Access-Control-Allow-Origin-Header. POSTMAN bypasses these checks (since it cannot currently visit any domain) and therefore always returns the response.

    What you should check to prevent this issue from happening is that you always include the client-side-domain in Access-Control-Allow-Origin (or simply use * to allow all origins). Make sure to also include it in OPTIONS requests since they are often sent prior to POST or PUT requests. If your requests are just failing on a certain HTTP-Method make sure to also set the Access-Control-Allow-Methods-Header to include all required HTTP-Methods (or simply use *)

    Since POSTMAN always accepts your request it’s a good tool to check if these headers are included in the response. If not, simply add them and you should be fine.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 Android STD快速启动
  • ¥15 如何使用simulink建立一个永磁同步直线电机模型?
  • ¥30 天体光谱图的的绘制并得到星表
  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动