Response.success未定义角度

时间:2016-02-19 09:51:36

标签: javascript java angularjs rest jersey

我有一个AngularJS Web应用程序,其RESTful Jersey Api作为后端。

我正在开发和测试这个api以创建用户:

function Create(user) {
        return $http.post('http://localhost:8080/NobelGrid/api/users/create/', user).then(handleSuccess, handleError('Error creating user'));
    }

这是API(POST)的代码:

/**
 * This API creates an user
 * 
 * @param data
 * @return
 */
@Path("create")
@POST
@Produces("application/json")
public Response create(String data) {

    UserDataConnector connector;
    JSONObject response = new JSONObject(data);

    User userToCreate = new User(response.getString("surname"), response.getString("name"), response.getString("mail"), response.getString("username"), response.getString("password"), 0);

    try {

        connector = new UserDataConnector();
        connector.createUser(userToCreate);

    } catch (IOException e) {
        e.printStackTrace();
        return Response.status(500) // 500
                .entity(e.getMessage()).header("Access-Control-Allow-Origin", "*")
                .header("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT").build();

    }

    return Response.status(200)// 200
            .header("Access-Control-Allow-Origin", "*")
            .header("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT").build();

}

/**
 * CORS compatible OPTIONS response
 * 
 * @return
 */
@Path("/create")
@OPTIONS
@Produces("application/json")
public Response createOPT() {

    return Response.status(200) // 200
            .header("Access-Control-Allow-Origin", "*")
            .header("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT, OPTIONS")
            .header("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, X-Codingpedia,Authorization")
            .build();
}

API运行良好,因为用户是在后端(在我自己的数据库中)创建的

enter image description here

但在角度网络应用中,响应结果为undefined

enter image description here

这些是我的凉亭配置:

 "dependencies": {
"angular": "1.4.x",
"angular-mocks": "1.4.x",
"jquery": "~2.1.1",
"bootstrap": "~3.1.1",
"angular-route": "1.4.x"}

我还有其他API(GET调用)运行良好(后端响应为200,Web应用程序到达200)。

你能帮我吗?

先谢谢。

2 个答案:

答案 0 :(得分:2)

您的后端代码没有任何正文回复。将ResponseBuilder static RemoteWebDriver firefoxDriver; static RemoteWebDriver someOtherDriver; static synchronized RemoteWebDriver getDriver(String browser, String browserVersion, String platform, String platformVersion) { if (browser == 'firefox') { if (firefoxDriver == null) { DesiredCapabilities cloudCaps = new DesiredCapabilities(); cloudCaps.setCapability("browser", browser); cloudCaps.setCapability("browser_version", browserVersion); cloudCaps.setCapability("os", platform); cloudCaps.setCapability("os_version", platformVersion); cloudCaps.setCapability("browserstack.debug", "true"); cloudCaps.setCapability("browserstack.local", "true"); firefoxDriver = new RemoteWebDriver(new URL(URL),cloudCaps); } } else { if (someOtherDriver == null) { DesiredCapabilities cloudCaps = new DesiredCapabilities(); cloudCaps.setCapability("browser", browser); cloudCaps.setCapability("browser_version", browserVersion); cloudCaps.setCapability("os", platform); cloudCaps.setCapability("os_version", platformVersion); cloudCaps.setCapability("browserstack.debug", "true"); cloudCaps.setCapability("browserstack.local", "true"); someOtherDriver = new RemoteWebDriver(new URL(URL),cloudCaps); } return someOtherDriver; } 添加到您的Response.it应该是这样的:

entity

答案 1 :(得分:1)

您没有在后端附加您的回复任何正文:

 return Response.status(200)// 200
        .header("Access-Control-Allow-Origin", "*")
        .header("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT").build();

您需要设置一个编写器并使用它来序列化您的对象:

ObjectMapper mapper = new ObjectMapper();
mapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false);
mapper.setVisibility(PropertyAccessor.FIELD, Visibility.PROTECTED_AND_PUBLIC);

ObjectWriter w = mapper.writer();
ResponseBuilder responseBuilder = Response
            .ok(w.writeValueAsString(userToCreate ));
responseBuilder.header("Access-Control-Allow-Origin", "*");
responseBuilder.header("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT");
return responseBuilder.build();

mapper-config可能与您的用例不同......