Отправка Jquery Ajax Json с Android на JAVA Backend - PullRequest
0 голосов
/ 06 августа 2020

У меня возникла проблема с отправкой JSON с устройства Android на мой Java Backend. Я не получаю никаких ответов (успех \ сбой).

Android:

    $('#add-user').on('click', function (){
        var user = {
            email: $email.val(),
            password: $password.val(),
        }
        $.ajax({
            url: 'http://10.0.2.2/costmanagement/rest/users/register',
            type: 'POST',
            data: user,
            success: function (isRegistrationSucceed, status){
                 console.log("POST SUCCESS");
            },
            error: function (){
                console.log("POST FAILED");
            }
        });
    })

Backend (путь / user):

    @POST
    @Path("/register")
    @Produces("text/html")
    @Consumes({"application/json"})
    public JSONObject registerUser(JSONObject jsonUser) {
        JSONObject userRegistration = new JSONObject();
        try {
            String email = jsonUser.getString("email");
            String password = jsonUser.getString("password");
            int id = jsonUser.getInt("id");
            User user = new User(email,password,id);
            userRegistration.put("isRegistrationSucceed", dao.registerUser(user));
            return userRegistration.put("status","OK");
        }
        catch (CostManagementException e){
            e.printStackTrace();
            userRegistration.put("status","ERROR");
            return  userRegistration.put("errorMessage",e.getMessage());
        }
    }

web. xml (просто чтобы показать, что путь существует):

        <url-pattern>/rest/*</url-pattern>

Сервер Tomcat запущен, и локальная регистрация на Backend работает. Я пытался отправить его с помощью stringify () - результат тот же. Адрес локального сервера: http://localhost:8080/costmanagement/

EDIT:

После создания простой строки POST, которая работает на бэкэнде по следующему адресу: http://localhost:8080/costmanagement/rest/user/hello

Пытался сделать то же самое на стороне Android, но с другим локальным хостом: http://10.0.2.2/costmanagement/rest/user/hello

Но при попытке получить к нему доступ через Android я получаю следующую ошибку:

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code 404.", source:
...