在Ajax Call之外更新变量

时间:2016-02-10 01:29:39

标签: php jquery ajax

我有一个变量settingId,用于确定我是进行POST还是进行PUT Ajax调用:             var settingId = form.data(' setting');

        if ((typeof settingId === "undefined")){
            method = 'POST';
            url = '{{ url("/tournaments") }}' + '/' + tournamentId + '/categories/' + categoryId + '/settings';
        }else{
            method = 'PUT';
            url = '{{ url("/tournaments") }}' + '/' + tournamentId + '/categories/' + categoryId + '/settings/' + settingId ;
        }

它工作得很好,但是当我做POST时,我需要更新这个变量值,这样同样的测试给了我一个PUT,我不能这样做。

现在,我这样做,但很明显,这不是正确的方法:

form.data['setting'] = data.settingId;

我的表格:

<form method="POST" action="http://laravel.dev/tournaments/3/categories/2/settings/11" accept-charset="UTF-8" class="form-settings" data-tournament="3" data-category="2" data-setting="11">

...    

JS:

$('.save_category').on('click', function (e) {
        e.preventDefault();
        var inputData = $('.save_category').serialize();
        var form = $(this).parents('form:first');

        var tournamentId =   form.data('tournament');
        var categoryId =   form.data('category');
        var settingId =  form.data('setting');

        console.log(tournamentId);
        console.log(categoryId);
        console.log(settingId);


        $(this).find('i').removeClass();
        $(this).find('i').addClass('icon-spinner spinner position-left');
        $(this).prop( "disabled", true );
        var panel = $(this).closest('.panel');
        var method = null;
        var url = null;

        if ((typeof settingId === "undefined")){
            method = 'POST';
            url = '{{ url("/tournaments") }}' + '/' + tournamentId + '/categories/' + categoryId + '/settings';
        }else{
            method = 'PUT';
            url = '{{ url("/tournaments") }}' + '/' + tournamentId + '/categories/' + categoryId + '/settings/' + settingId ;
        }
        $.ajax(
                {
                    type: method,
                    url: url,
                    data: inputData,
                    success: function (data) {
                        if (data != null && data.status == 'success') {
                            noty({
                                layout: 'topRight',
                                type: 'success',
                                width: 200,
                                dismissQueue: true,
                                timeout: 3000,
                                text: data.msg
                            });
                            // Change warning icon to success
                            panel.find('.status-icon').removeClass().addClass('glyphicon glyphicon-ok text-success status-icon');
                            console.log(data);
                            form.data['setting'] = data.settingId;

                        } else {
                            noty({
                                layout: 'topRight',
                                type: 'error',
                                width: 200,
                                dismissQueue: true,
                                timeout: 3000,
                                text: data.msg
                            });
                        }
                        $('.save_category').prop( "disabled", false );
                        $('.save_category').find('i').removeClass('icon-spinner spinner position-left');




                    },
                    error: function (data) {
                        noty({
                            layout: 'topRight',
                            type: 'error',
                            width: 200,
                            dismissQueue: true,
                            timeout: 3000,
                            text: data.msg
                        });
                        $('.save_category').prop( "disabled", false );
                        $('.save_category').find('i').removeClass('icon-spinner spinner position-left');
                    }

                }
        )

    });

任何想法????

2 个答案:

答案 0 :(得分:1)

只需使用jQuery:

$('form .form-settings').attr('data-setting', data.settingId);

答案 1 :(得分:1)

尝试

Nagios/Zabbix/Dynatrace