简体   繁体   中英

how to fix the java script function that it get a json parameters and send it to the url

I have a java program for coupon system and one of the methods is to update company details the code in java:

    public Company updateCompanyDetailes(Company companyToUpDate) throws CustomException {
    Company companyFromDb = companyRpository.findById(companyToUpDate.getId()).get();

    companyFromDb.setPassword(companyToUpDate.getPassword());
    companyFromDb.setEmail(companyToUpDate.getEmail());
    companyToUpDate = companyRpository.save(companyFromDb);

    return companyToUpDate;
}

It works great but when i want to call this method from my HTML page the javascript is crashing all the time and not transfer the company details as an object,

$scope.companyToUpDate = function() {
    $scope.hideAll();
    $scope.UpdateComp = true;
    $scope.executeForCompany = function() {
        if ($scope.id == '') {
            window.alert("You must enter ID")
        }else {
            alert("do you want update company?");
                var id = $scope.id;
                var password = $scope.password;
                var email = $scope.email;

                var company = {
                companyId: id,
                email: email,
                password: password
            };
            $http.put('http://localhost:8999/rest/api/admin/updateCompany', company)
                .then(function (response){
                    window.alert('updated');
                    // $scope.id = '';
                    //$scope.password = '';
                    // $scope.email= '';
                    $scope.hideAll();
                $scope.WelcomePage =true;


                }, function(error) {
            alert('operation failed' + error.data);
                });
                }
                }
                }

As I try is rest when I send aJASON with the parameters id , password , and this is the HTML code:

<div class="col-md-9" ng-show="UpdateComp">
            <h2>choose company id to update:</h2>
            <input type="number" ng-model="id" min="1">ID</input>
            <h4>fill the fields (Password is mandatory):</h4>
            * Password<input type="text" ng-model="password">
        </input>
            Email<input
                type="text" ng-model="email"></input>
            <button type="button" class="btn btn-primary"
                ng-click="executeForCompany()">UPD</button>
                <div ng-show="UpdateComp" ng-model="company">
        </div>
        </div>

i am getting the companyId from the user, all the Javascript are working beside this one

i get this error:

java.lang.IllegalArgumentException: The given id must not be null

When you pass data in json from javascript and are catching that json in a Object(Company) then you need to be careful that the keys are the same as defined in class level.

There is an error for id, check that are you getting you data here

var id = $scope.id;
var password = $scope.password;
var email = $scope.email;

by printing or any means you use.

Also in $http.put('http://localhost:8999/rest/api/admin/updateCompany', company) you passing object named company but receiving it as companyToUpDate make them same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM