简体   繁体   中英

How to use AJAX function in Total js?

I have a User form and I am trying to do a POST. Earlier code with schemas work. I am fitting in AJAX just now and I dont have a clue why it is not working. There is no error.

Below is code snippet from users.html file

</div>
            <div class="btn" role="group">
                <button type="button" name="submit" id="Submit" class="btn btn-primary" onclick="onSubmit()">Submit</button>
            </div>
            <div class="btn" role="group">
                <input class="btn btn-warning" type="reset" value="Reset">
            </div>
        </form>
        <script type="text/javascript">
            function onSubmit() {
                console.log("Ajax Testing");
                AJAX('POST /users', function (value, err, response) {
                    if (err) { throw err };
                    console.log(value);
                    console.log(response);
                });
            };
        </script>

Here is the routing part from default.js file

exports.install = function () {
    ROUTE('GET  /', displayHome);
    ROUTE('GET  /{userId}   *userSchema --> @query');
    ROUTE('GET  /homepage', displayHome);
    ROUTE('POST /users      *userSchema --> @insert');

Below is the schema for Insert operation

schema.setInsert(function ($) {
        var userDB = DATABASE('users');
        userObj = {
            name: $.controller.body.name,
            age: $.controller.body.age,
            height: $.controller.body.height,
            email: $.controller.body.email,
            gender: $.controller.body.gender,
            cities: $.controller.body.cities,
            landline: $.controller.body.telephone,
            about: $.controller.body.aboutMe
        };
        userDB.insertOne(userObj, function (err, result) {
            if (err) throw err;
            console.log("Created New user using Schema Definition" + result);
            $.controller.view('homepage');
        }.bind($.controller));
    });

Can you please let me know what is the error?

You forgot to send a data:

AJAX('POST /users/', YOUR_DATA, function (value) {
    console.log(value);
});

BTW: the code is not good in your schema. You need to work with fields defined in the schema and then everything is much simpler:

schema.setInsert(function ($) {
    DB('users').insertOne($.clean(), $.callback);
});

Learn from examples:

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