1. 程式人生 > >Ajax資料新增與更新

Ajax資料新增與更新

        var hotel = {
            UserName: '111111',
            Id: id,
            Name: $('#hotelName').val(),
            Logo: img,
            BackgroundColor: $('#backgroundColor').val(),
            LangList: [{
                Active: $enlang,
                LangCode: 'en',
                Message: $('#Content-en').html()
            }, {
                Active: $frlang,
                LangCode: 'fr',
                Message: $('#Content-fr').html()
            }, {
                Active: $selang,
                LangCode: 'se',
                Message: $('#Content-se').html()
            }]
        };

        var _formData = new FormData();
        _formData.append("hotel", JSON.stringify(hotel));

        var path = $("#showimg").html();
        if (repath.length != 0 || id == null) {
            _formData.append('ImagePath', document.getElementById("txtImagePath").files[0]);
        }

        var $url = null;
        if (id == null) {
            $url = _url;
        } else {
            $url = _url + "/" + id;
        }

        $.ajax({
            url: $url,
            type: 'POST',
            cache: false,
            data: _formData,
            processData: false,
            contentType: false
        }).done(function (res) {
            window.location.reload();
            console.log('success,' + res);
        }).fail(function (res) {
            console.log('fail,' + res);
        });
    });