我有一個變量settingId,用於確定我是否會執行POST或PUT Ajax調用: var settingId = form.data('setting');Ajax調用之外的更新變量
if ((typeof settingId === "undefined")){
method = 'POST';
url = '{{ url("/tournaments") }}' + '/' + tournamentId + '/categories/' + categoryId + '/settings';
}else{
method = 'PUT';
url = '{{ url("/tournaments") }}' + '/' + tournamentId + '/categories/' + categoryId + '/settings/' + settingId ;
}
它的偉大工程,但是當我做一個帖子,我需要更新這個變量的值,所以這個相同的測試給了我一個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');
}
}
)
});
任何標識EA ????
你能顯示完整的你在哪裏調用上面的腳本javascript代碼? –
是的,在一分鐘內 –
我更新了問題! –