jQuery-AJAX: Checking if Data Exists, if not “POST”

jQuery-AJAX: Checking if Data Exists, if not “POST”

I am trying to check if the data already exists in the JSON Array Object that is returned on 'GET'. IF the data already exists, I want it to ignore the data. If it does not exist I want it to "POST." Please help. It seems to be skipping the "POST" call.


  1. function POST(section){ $.ajax({ type: 'POST', dataType: 'json', async : false, data: JSON.stringify(section), url: '/api/v2/help_center/en-us/categories/200384077/sections.json', contentType: "application/json; charset=utf-8", headers: { "Authorization": "Basic " + btoa(username + ":" + password) },// end headers success: function (newSection) { console.log("Congrats! We have inserted a new section", newSection); }, error: function (xhr, errorText) { console.log('Error ' + xhr.responseText); } })// end AJAX POST } function addNewSection(name, description) { var section = { "section": { "position": 2, "name": name, "description": description, "content.translations.name": [{ "locale": "en-us" }] } }; $.ajax({ type: 'GET', url: '/api/v2/help_center/categories/200384077/sections.json', dataType: 'json', async : false, headers: { "Authorization": "Basic" + btoa(username + ":" + password) }, success: function (mySections) { var naming = mySections.sections; $.each(naming, function (i, result) { if( $.inArray(name, result) == -1) { console.log("This already exists", name); } if( !$.inArray(name, result) == -1) { POST(section); } }); } // end success function })// end AJAX GET }