【问题标题】:No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'xxx' is therefore not allowed access请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,Origin 'xxx' 不允许访问
【发布时间】:2017-02-04 23:48:56
【问题描述】:

我尝试将 paypal 支付网关集成到我的应用程序中。

我只是使用 angularjs get 方法调用 java webservice。

下面是angularjs http get方法代码

  $http({
            method : 'GET',
            url : 'http://localhost:8080/xxxx/redirect'
        }).then(function successCallback(response) {
            console.info("success");
        });

尝试的Java端代码是-

   @RequestMapping(value = "/redirect", method = RequestMethod.GET, headers = "Accept=application/json")
    public ModelAndView method(Payment pPayment) {
     String amount=pPayment.getAmount();
     String url="https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_xclick&business=wipro-seller@gmail.com&first_name=smartCity&city=Madurai&state=Tamilnadu&item_name=shirt&amount=10&night_phone_a=9513335437&item_name=shirt&address1=wipro-seller@gmail.com&business=k.tapankumar@gmail.com&quantity=1&currency=USD";
            return new ModelAndView("redirect:" + url);

    }

我仅从 Java 服务器端调用 Paypal 测试帐户 url,但仍然出现以下错误

  XMLHttpRequest cannot load https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_xclick&business=wipro-se…o-seller@gmail.com&business=k.tapankumar@gmail.com&quantity=1&currency=USD. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access.

请告诉我我哪里做错了..

【问题讨论】:

  • 这是一个 CORS 错误,您可能需要在服务器端进行更改以包含响应标头。看流程图here可能会帮助你理解CORS

标签: java angularjs web-services paypal get


【解决方案1】:

您需要为您的 HTTP 请求启用跨源请求 (CORS)。

Spring 文档文档说明了如何很好地实现这一点。找到它here。我建议您通读并尝试一下。如果还是不行,就在这里发帖吧。

更多关于 CORS here.

【讨论】:

    【解决方案2】:

    您也应该允许来自客户端的跨源请求。

    安装跨域的chrome插件后尝试运行

    https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi?hl=en

    这是我使用的链接。

    你也应该改变你的标题以允许这样的cors

    Access-Control-Allow-Headers: Content-Type
    Access-Control-Allow-Methods: GET, POST, OPTIONS
    Access-Control-Allow-Origin: *
    

    【讨论】:

      猜你喜欢
      • 2013-12-24
      • 2014-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-09
      • 2018-10-15
      • 2014-03-13
      相关资源
      最近更新 更多