user3686281
user3686281

Reputation: 3

How to set json data into ajax response

I want to set ajax response data in dropdown.my demo code for ajax call is below.

function  onChangeajax(e)
    {
        griddataObj = $('#ObjectAccountConfiguration').data("kendoGrid");
        accountID = e.container.find(":input[name=AccountID]");
        accountIDdropdown = accountID.data("kendoDropDownList");
        var x = contactType.val();
        var y = accountType.val();
        var z = LedgerType.val();
        var a = contactGroupID.val();

        $.ajax
        ({
          type:'post', 
          data: { 'x': x, 'y': y, 'z': z, 'a': a },
          url : '<?php echo $this->Html->url(array("controller"=>"ObjectTypes/getAccount")); ?>',
          success: function(data)
          {

          }
        });
    }

In ajax response i get

{"data":[{"value":"87","text":"Expense"}]}

Now how to set this data in ajax sucess??

Upvotes: 0

Views: 340

Answers (2)

Jitendra Yadav
Jitendra Yadav

Reputation: 896

Try this this will alert your select box. Further you can use var str to display on your page

function  onChangeajax(e)
        {
            griddataObj = $('#ObjectAccountConfiguration').data("kendoGrid");
            accountID = e.container.find(":input[name=AccountID]");
            accountIDdropdown = accountID.data("kendoDropDownList");
            var x = contactType.val();
            var y = accountType.val();
            var z = LedgerType.val();
            var a = contactGroupID.val();
            var str='<select name="account">';

            $.ajax
            ({
              type:'post', 
              data: { 'x': x, 'y': y, 'z': z, 'a': a },
              url : '<?php echo $this->Html->url(array("controller"=>"ObjectTypes/getAccount")); ?>',
              success: function(data)
              {
                $.each(data[data],function(key,val){
                   str+='<option value="'+val['value']+'">'+val['text']+'</option>';

                   });
                str+='</select>';
                alert(str);
              }
            });
        }

Upvotes: 1

serakfalcon
serakfalcon

Reputation: 3531

the 'success' parameter of $.ajax is a function that is called after the AJAX response is received from the server. It has 3 parameters, data, textStatus and jqXHR. textStatus and jqXHR are more useful for assessing whether the response is valid and is what you expected to get, while data is the response.

In your case, since your response is

{"data":[{"value":"87","text":"Expense"}]}

data.data.value is equal to 87. Inside the function, you can use that value for whatever you want. Understand?

Upvotes: 1

Related Questions