i trying check if data exists in json array object returned on 'get'. if data exists, want ignore data. if not exist want "post." please help. seems skipping "post" call.
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! have inserted 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 exists", name); } if( !$.inarray(name, result) == -1) { post(section); } }); } // end success function })// end ajax }
remove ! this:
if( !$.inarray(name, result) == -1) so have:
if ($.inarray(name, result) == -1) what wrote double negative. $.inarray returns -1 when doesn't find anything. never use ! $.inarray because can never return false.
Comments
Post a Comment