jquery fill dropdown with json data

后端 未结 4 1391
眼角桃花
眼角桃花 2020-11-28 06:51

I have the following jQuery code. I am able to get the following data from server [{\"value\":\"1\",\"label\":\"xyz\"}, {\"value\":\"2\",\"label\":\"abc\"}]. Ho

4条回答
  •  无人及你
    2020-11-28 07:54

    This should do the trick:

    $($.parseJSON(data.msg)).map(function () {
        return $('

    Here's the distinction between ajax and getJSON (from the jQuery documentation):

    [getJSON] is a shorthand Ajax function, which is equivalent to:

    $.ajax({
      url: url,
      dataType: 'json',
      data: data,
      success: callback
    });
    

    EDIT: To be clear, part of the problem was that the server's response was returning a json object that looked like this:

    {
        "msg": '[{"value":"1","label":"xyz"}, {"value":"2","label":"abc"}]'
    }
    

    ...So that msg property needed to be parsed manually using $.parseJSON().

提交回复
热议问题