起源' http:// localhost:8080'因此不允许访问。响应的HTTP状态代码为403

时间:2017-06-08 06:11:02

标签: javascript angularjs rest spring-boot cors

我正在尝试将一个端口中的Angular Js代码连接到另一个端口上的RESTFul服务。我们正在使用弹簧靴。

我收到此错误: - > No' Access-Control-Allow-Origin'标头出现在请求的资源上。 - > GET http://localhost:8181/newControl 403()。

我们通过在角度js和服务器端添加标头来尝试所有方法。仍然无法解决问题。

myController.js

$scope.getDataFromServer = function() {

    $http({
        method : 'GET',
        url : 'http://localhost:8181/newControl',
        headers: {
            'Content-Type': 'application/json; charset=utf-8',
                'Access-Control-Allow-Origin': '*'
} 
    }).success(function(data, status, headers, config) {
        console.log("Data is :");
        console.log(data);
        $scope.person = data;
        $scope.update();
        //$('#specializationId').find('select').trigger('change');
        //angular.element(e.target).siblings('#upload').trigger('click');
    }).error(function(data, status, headers, config) {
        // called asynchronously if an error occurs
        // or server returns response with an error status.
    });

};

NewController.java

@RestController
public class NewController {

@RequestMapping("/newController")
public String firstPage() {

    return ("Hello... Welcome to Springboot");
}

@CrossOrigin(origins = "http://localhost:8181")
@RequestMapping("/newControl")
public PersonData getPersonData(final HttpServletResponse response) {


    response.setHeader("Access-Control-Allow-Origin", "*");
    response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
    response.setHeader("Access-Control-Max-Age", "3600");
    response.setHeader("Access-Control-Allow-Headers", "x-requested-with");


    PersonData person = new PersonData();
    Adress address = new Adress();
    person.setFirstName("Adam");
    person.setLastName("hewad");
    address.setCity("XYZ");
    address.setCountry("ABC");
    address.setState("pqrs");
    person.setGender("Male");
    person.setSpecialization("Dev");
    person.setAddress(address);

    return person;
}

}

0 个答案:

没有答案