Javascript 如何在函数中创建循环,然后将其发送到JSON文件
所以,我有我的JSON文件,但我需要通过一个循环向我的JSON发送一个特定的URL 这就是我设置一切的地方:Javascript 如何在函数中创建循环,然后将其发送到JSON文件,javascript,ajax,Javascript,Ajax,所以,我有我的JSON文件,但我需要通过一个循环向我的JSON发送一个特定的URL 这就是我设置一切的地方: function setMainObjectArray() { var exercises = []; var eBlocks = $('.eBlock'); eBlocks.each(function(i, eBlock) { var exObject = { word: $(eBlock).find('input.Ex
function setMainObjectArray() {
var exercises = [];
var eBlocks = $('.eBlock');
eBlocks.each(function(i, eBlock) {
var exObject = {
word: $(eBlock).find('input.ExerciseGetWordInput').val(),
syllables: []
};
$(eBlock).find('input.syllable').each(function(j, syll) {
exObject.syllables.push($(syll).val());
});
exercises.push(exObject);
});
return exercises;
}
这是我的ajax保存功能:
function saveExerciseAjaxCall() {
console.log(setMainObjectArray());
$.ajax({
url: 'saveJson.php',
type: 'POST',
data: {
id: getUrlParameter('id'),
getExerciseTitle: $('#getExerciseTitle').val(),
language: $('#languageSelector').val(),
application: 'lettergrepen',
'main_object': {
title: $('#getExerciseTitle').val(),
language: $('#languageSelector').val(),
exercises: setMainObjectArray()
},
dataType: 'json'
}
}).done(function(response) {
}).fail(function(jqXHR, textStatus, errorThrown) {
console.log(jqXHR);
console.log(errorThrown);
console.log(textStatus);
});
}
我想我需要通过我的函数setMainObjectArray()
但是我不知道如何在我的函数
setMainObjectArray
中实现这一点。我不完全确定循环是否是以正确的方式编写的,我对javascript非常陌生,从那时起,我会将它放在我的ajax调用中(因为它是一个链接)。您能否修复标题,目前看起来您希望将循环存储到JSON文件中。“从那时起,我会将它放在我的ajax调用中(因为它是一个链接)。”…实际上它只是一个包含URL的字符串。至于你的循环是否正确,你还没有解释你实际上想要实现什么,所以很难确定。你自己试过吗?
var audioId = MEDIAARRAY.audio.lowercase.indexOf(exObject['exerciseGetWordInput'].toLowerCase() + '.mp3');
var audio_link = '';
if (audioId > -1) {
exObject['audio'] = 'https://TheUrlServer' + MEDIAARRAY.audio.path + MEDIAARRAY.audio.files[audioId];
}