1

this is my code :

$(function(){
    $("div#SearchBox").ready(function(){
    var selectsCount;
        $.getJSON("http://localhost/cms/publicity/list/jsonoptions/", function(result){
            selectsCount = (result.length);
        })
        alert(selectsCount);
    })
})

the result is undefined , why ?!

or this code :

$(function(){
    $("div#SearchBox").ready(function(){
        $.getJSON("http://localhost/cms/publicity/list/jsonoptions/", function(result){
            $.getJSON("http://localhost/cms/publicity/list/jsonoptitems/color/", function(secondResult){
            var selectBoxes = '';
                for (var i = 0; i < result.length; i++) {
                    selectBoxes += '<select id="'+result[i].OptionValue+'">';
                    selectBoxes += '<option value="'+result[i].OptionValue+'">'+result[i].OptionDisplay+'</option>';
                    selectBoxes += '</select>';
                }
            $("div#SearchBox").html(selectBoxes);
            })
        })
    })
})

it's not working too ...

infact I wana to get selects from a json file , then get that select's options from another json file with getJSON function in Jquery

so I want to do it .

please help me , thank you a lot :x

RezA
  • 19
  • 4
  • First check what `result` gives inside the scope? – Dhaval Marthak May 30 '15 at 02:55
  • @DhavalMarthak , everythings It's ok , that code executed successfully without return or redeclare variable's value inside the getJSON – RezA May 30 '15 at 02:58
  • http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call – Arun P Johny May 30 '15 at 03:11
  • @ArunPJohny thank you for your reply , I seen that article but I can't understand it , If you (or any people) can , please edit my code and resolve my problem , thank you bro. – RezA May 30 '15 at 03:14
  • @RezA Can create jsfiddle http://jsfiddle.net to demonstrate ? – guest271314 May 30 '15 at 03:48
  • @guest271314 emmm , thats json responsive files are in local server , so It is not a good idea to executes codes at jsfiddle.net , the problem is using jquery getJSON in side another getJSON not anymore ... – RezA May 30 '15 at 04:18
  • @RezA jsfiddle permits echo of `JSON` https://jsfiddle.net/2uen1t8f/ . Tried changing reference to current item within `$.getJSON()` , where both `$.getJSON` functions appear to have `result` as name of current item within iteration ? – guest271314 May 30 '15 at 04:26
  • @guest271314 oh sorry , I changed that second parameter to secondResult , I try to get id and value of html selects from a json file and make thats in a `for` loop , and get options of thats select from another json file , just it , no anymore ... – RezA May 30 '15 at 04:34
  • @RezA Yes . If possible , try including `html` , `JSON` at "jsonoptions/" as `var json1` , `JSON` at "jsonoptitems/color/" as `var json2` ; utilize same pattern as at Question substituting `$.post()` for `$.getJSON` at https://jsfiddle.net/2uen1t8f/ ; click "Update" ? – guest271314 May 30 '15 at 04:39
  • @guest271314 emmm , It's a good idea , I can merge 2 jsons and send 1 request ... tnq ;) – RezA May 30 '15 at 05:17

0 Answers0