Dan
Dan

Reputation: 2174

how to return json result in jquery function from action class

My aim is to display customer info by costumer id by calling my strus2 action change of selectbox value.

My problem is: what should i return from my action class to get the value in json format

I tried the following code, but i don't know what is wrong with it

    <script type="text/javascript">  
  $(function()
   {    alert("This function is calling, on change event of selectbox and customer id="+$(this).val());
     $("#customersjsonlstid").change(function(e)
         { 
                    $.ajax({
                        url: 'showCustomerInfoById.action',  //action name
                        data: "customerid=" + $(this).val(),    
                        dataType: "json",
                        success: function(data) {    

                            // Set the inputs from the json object , json output : {"price": "123.40", "distributor": "XYZ Inc."}          
                            $('#autocompleter_div_result1').val(data.distributor); //showing data on particular div
                            $('#autocompleter_div_result2').val(data.price);  //showing data on particular div
                        }
                    });
                });
    });             
</script>

Selectbox:

                            <sj:select 
                                id="customersjsonlstid" 
                                name="editionType" 
                                href="%{customerJsonselecturl}" 
                                list="lstcust"
                                listValue="name" 
                                listKey="id"   
                                autocomplete="false"
                            />     
             <div id="autocompleter_div_result">`

struts.xml

        <action name="showCustomerInfoById" class="v.esoft.actions.customerdetails.CustomerSelectAction" method="CustomerdetailsById">
             <result name="success" type="json"/> 
        </action> 

Action class

    public class CustomerSelectAction extends ActionSupport {

     private String  customerid;


//---------------------------------------------------------
   public String CustomerdetailsById()
    {
        System.out.print("--------Method called, customer id-----------"+customerid);
        return customerid;
    }
     //---------------------------------------------------------


public String getCustomerid() {
    return customerid;
}


public void setCustomerid(String customerid) {
    this.customerid = customerid;
}

  }

Upvotes: 1

Views: 2527

Answers (2)

Tushar
Tushar

Reputation: 1470

I faced the same issue and finally it got resolved. you can try like this :

$("#customersjsonlstid").change(function(e)
         { 
         var   customeridJson =    $('#customersjsonlstid').val();
                    $.ajax({
                        url: 'showCustomerInfoById',  //action name   same name in struts.xml
                        data: "customerid=" + customeridJson,    
                        dataType: "json",
                        async: true,
                        success: function(data) {    

                            // Set the inputs from the json object , json output : {"price": "123.40", "distributor": "XYZ Inc."}          
                            $('#autocompleter_div_result1').val(data.distributor); //showing data on particular div
                            $('#autocompleter_div_result2').val(data.price);  //showing data on particular div
                        }
                    });
                });
    }); 

I did the same and it worked for me. I hope this works for you as well.

Upvotes: 1

Jaiwo99
Jaiwo99

Reputation: 10017

Try this:

<action name="showCustomerInfoById" 
        class="v.esoft.actions.customerdetails.CustomerSelectAction">
             <result name="success" type="json"/> 
</action>

Hint: removed attribute: method = "CustomerdetailsById"

@Override
public String execute() {
    return SUCCESS;
}

In this case, you will get a json object like this:

{
    "customerId":"value(customerId)"
}

You can also call showCustomerInfoById.action in your browser, you should see the json shows in the browser.

Upvotes: 1

Related Questions