1

This is my JavaScript code:

// action when item file is clicked
$("li span.file").click(function(){

    // get the ID
    console.log($(this).attr('id'));

    $.getJSON('BomItemToJSON', function(data) {
        $.each(data, function(i, item) {
            var id = item.id;
            var description = item.description;

            formObject = document.forms['itemForm'];
            formObject.elements['itemId'].value = id;
            formObject.elements['itemDescription'].value = description;
        });
    });

});

This is a part of my servlet:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        utx.begin();

        BomHandling bh = new BomHandling(em, utx);

        BomItem item = bh.getBomItem(63788);
        Gson gson = new Gson();
        String json = gson.toJson(item);

        System.out.println("Json: " + json);

        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(json);
        utx.commit();
    }

It returns:

Json: {"itemId":63788,"modules":[],"deprecated":false,"partNumber":"SG-XPCIE8SAS-E-Z","description":"4 GB Memory Expansion (2 x 2GB) low-profile FBDIMMs, Gen 2, 1.8 V, for Sun SPARC Enterprise, RoHS 6. (For Factory Integration Only)","quantity":0,"unitPriceDollar":"$350.00","discount":"10%","totalDollar":"$0.00","itemClass":"Server","itemType":"HW","vendor":"Sun"}

From the console log I know that this servlet is called:

[09:22:11.633] GET http://localhost:8084/xxx/BomItemToJSON [HTTP/1.1 200 OK 80ms]

This is my form:

<div id="itemdetail">
    <form name="itemForm">
        ID: <input type="text" name="itemId" value="" size="100"></input>
        Description: <input type="text" name="itemDescription" value="" size="100"></input>
    </form>
</div>

How can I insert the itemId and the description from the JSON object into my form? Right now it is always 'undefined'.

2 Answers 2

4

You don't need the each. Your JSON is only a single object, not an array.

Your code should be like this I think:

// action when item file is clicked
$("li span.file").click(function(){

    // get the ID
    console.log($(this).attr('id'));

    $.getJSON('BomItemToJSON', function(data) {
        alert('entered getJSON()');
        var id = data.itemId;
        var description = data.description;

        alert('description: ' + description);

        formObject = document.forms['itemForm'];
        formObject.elements['itemId'].value = id;
        formObject.elements['itemDescription'].value = description;

        alert('done with javascript');
    });

});
Sign up to request clarification or add additional context in comments.

Comments

1

a JSON object should be wrapped inside brackets: { Json: { ... } } if you try to validate your json with jsonLint you can notice the error

3 Comments

I thought about this, not clear on whether the OP is saying "this is my Json:" or that's part of the code.
:) before starting to see code errors on code that deals with JSON, I usually double-check the structure returned
Looking at his servlet code, it looks like that's the output of this line: System.out.println("Json: " + json); so probably not part of the code.

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.