0

Hi I am receiving a JSON list in the Ajax success block and I want to assign these values to a dropdown list potentialFailureModeList present on my JSP. I am new to JSON, i have tried a lot to findout on the net but did not get anything. Please help me. Any useful link will also work.

//Code from JSP

<td><select name="fmeaEntityForm[0].potentialFailureMode.id"
                            id="potentialFailureMode0" onchange="potentialFailureModeText(this)">
                                <option value="-1"><spring:message code="label.fmea.select.potentialFailureMode" /></option>
                                <c:forEach items="${potentialFailureModeList}" var="pfm">
                                    <option value="${pfm.id}">${pfm.potentialFailureMode}</option>
                                </c:forEach>
                        <option value="0"><spring:message code="label.fmea.select.other" /></option>
                        </select></td>

//pattern receiving in ajax

{"potentialFailureModeList":[{"id":3,"potentialFailureMode":"potentialFailureMode1","remark":"1"},
             {"id":4,"potentialFailureMode":"potentialFailureMode2","remark":"2"}]}

//Ajax method function getpotentialFailureModeList(elementIdIndex) {

        if (-1 != document.getElementById("subSystem"+elementIdIndex+"").value)  {

            $.ajax({
                type: "GET",
                url: '/ISO26262/FmeaDocumentPage/potentialFailureModeList',
                data: ({subSystemId : $('#subSystem'+elementIdIndex+'').val() }),
                success: function(items) {
                    alert("success");
                    alert(items);
                    // to do task
                },
                error: function (e) {
                    alert('Error Received: ' + e);
                  },
            });
        } 
}
2
  • This can be done in a variety of ways - you should narrow this down to a more specific problem. Commented Dec 1, 2015 at 10:44
  • @FritzDuchardt I will be very thankful to you if you would provide me the links of the various existing methods.. Commented Dec 1, 2015 at 11:06

2 Answers 2

1

Thanks a lot for replying. I got the answer. I am posting here, so that if anyone else need the same.

function getpotentialFailureModeList(elementIdIndex)  {

        if (-1 != document.getElementById("subSystem"+elementIdIndex+"").value)  {

            $.ajax({
                type: "GET",
                url: '/ISO26262/FmeaDocumentPage/potentialFailureModeList',
                data: ({subSystemId : $('#subSystem'+elementIdIndex+'').val() }),
                dataType:'json',
                success: function(items) {
                    var list = items;
                    $.each(list.potentialFailureModeList, function (index, item) {
                        $('#potentialFailureMode'+elementIdIndex+'').append($('<option>', {value: item.id, text: item.potentialFailureMode}));
                        })
                },
                error: function (e) {
                    alert('Error Received: ' + e);
                  },
            });
        } 
}
Sign up to request clarification or add additional context in comments.

Comments

0

json should be in format of

 {id: x, value: y}

Then the ajax success function similar to this --

success: function (items) {
    $.each(items, function (index, item) {
    $('#potentialFailureMode0').append($('<option>', {value: item.id, text: item.potentialFailureMode}));
});

2 Comments

thanks a lot for your reply. Is there any method to convert my received string into the format you have mentioned. Because in the format you have mentioned double quotes are not present on the attributes.
that i already corrected. but I am getting error in browser console: Uncaught TypeError: Cannot use 'in' operator to search for 'length' in {"potentialFailureModeList":[{"id":3,"potentialFailureMode":"potentialFailureMode1","remark":"1"},{"id":4,"potentialFailureMode":"potentialFailureMode2","remark":"2"}]}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.