2016-09-19 4 views
3

Spring bootRESTful APIを作成し、Pivotal web servicesでホストします。Spring RESTfulでPivotal Webサービスでホストされている 'Access-Control-Allow-Origin'エラーがありません

のは、URLがhttps://abc.cfapps.io/studentsで言ってみましょうとJSONの結果は、その後

[ 

     {"id":1,"name":"Michael","score":8.5}, 
     {"id":2,"name":"Naomi","score":5.6} 
] 

私はそのURLへのリクエストを送信するために角度クライアントを書くようになります。

angular.module("app", []).controller("listController", function($scope, $http) 
{ 
    var url = 'https://abc.cfapps.io/students'; 
    var httpRequest = new XMLHttpRequest(); 
    httpRequest.open('GET', url, true); 
    httpRequest.setRequestHeader('Access-Control-Allow-Origin', '*'); 
    httpRequest.setRequestHeader('Content-Type', 'application/json'); 
    httpRequest.onerror = function (XMLHttpRequest, textStatus, errorThrown) { 
     console.log('failed'); 
     console.log(JSON.stringify(XMLHttpRequest)); 
    }; 
    httpRequest.onload = function() { 
     console.log('SUCCESS!'); 
    } 
    httpRequest.send();   
}); 

localhost:52442で私のクライアントの実行と私のSpring boot serviceでもCORSも許可します。

@RestController 
@CrossOrigin(origins = "http://localhost:52442") 
@RequestMapping(value="/students") 
public class StudentService 
{ 

    @RequestMapping(value="/",method = RequestMethod.GET) 
    public ArrayList<Student> getListStudents() 
    { 
     // return list 
    } 

// other methods 
} 

しかし、私はこのエラーを取得しておいてください。

XMLHttpRequest cannot load https://abc.cfapps.io/students.   
Response to preflight request doesn't pass access control check:  
No 'Access-Control-Allow-Origin' header is present on the requested resource.   
Origin 'http://localhost:52442' is therefore not allowed access. The response had HTTP status code 403. 

答えて

0

ヘッダーのあなたがでJavaコードを使用している場合は、クロスオリジンに

$http({method: 'GET', 
      url: 'https://abc.cfapps.io/students', 
      cache:false, 
      headers: { 
       'Access-Control-Allow-Origin': '*', 
       'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS', 
       'Access-Control-Allow-Headers': 'Content-Type, X-Requested-With', 
       'X-Random-Shit':'123123123' 
      }}) 
      .success(function(outputData) { 
    }); 
5

を許可するように設定して$http GET呼び出しの下のフォーマットをお試しくださいバックエンド

このようにして、それは

package com.web; 

    import org.springframework.stereotype.Component; 

    import javax.servlet.*; 
    import javax.servlet.http.HttpServletResponse; 
    import java.io.IOException; 

/** 
* Note this is a very simple CORS filter that is wide open. 
* This would need to be locked down. 
*/ 
@Component 
public class CORSFilter implements Filter { 

    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { 
     HttpServletResponse response = (HttpServletResponse) res; 
     response.setHeader("Access-Control-Allow-Origin", "*"); 
     response.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, DELETE"); 
     response.setHeader("Access-Control-Max-Age", "3600"); 
     response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); 
     chain.doFilter(req, res); 
    } 

    public void init(FilterConfig filterConfig) {} 

    public void destroy() {} 

} 

私はこれが役に立つかもしれないと思う

関連する問題