0

I'm using two jsp one for searching the records and another for updating the records of the database. Search query will return a json object where it contain all the searched records and I'm generating the rows dynamically to a table for displaying. To update a record, I've kept link in every row. Here I have to pass the json object to the updating jsp file to load the values. So, I simply passed the json object. But, I cannot process the json object. Help me out. Thanks. Please Find the code below:

function retrievesrchrec(){                 
    var result = xmlHttp.responseText;
    if(result.length>1){
        var dbRecords = JSON.parse(result);
        if(dbRecords)
        {
            while(dbRecords[index])
            {
                dbRecord= dbRecords[index];
                var rbutton = document.createElement("input");
                rbutton.type = "a";
                rbutton.href = function(){
                    javascript:loadModify(jsonobj);
                };

            }
        }
    }
}
function loadmodify(jsonobj){
    alert(jsonobj);
}
Developer404
  • 5,716
  • 16
  • 64
  • 102
  • similar question on SO : http://stackoverflow.com/questions/829643/processing-json-objects-in-jsp – naiquevin May 03 '11 at 05:37
  • Some more details are needed, like whether you use the data on the server side or on the client side, and what kind of data it really is. JSON data is not an object, it's a string, so do you have an object, or do you have JSON? – Guffa May 03 '11 at 05:38
  • I've parsed and I'm iterating the json returned. Everything is done through javascript only. Please see the code – Developer404 May 03 '11 at 05:42
  • What's the output of `console.log(result);`? – Aleadam May 03 '11 at 05:43
  • Just Object[Object] for printing the jsonobj. When I tried to retrieve the content jsonobj["ID"]. I got undefined as the value. – Developer404 May 03 '11 at 05:45
  • @Nila that does not answer my question – Aleadam May 03 '11 at 05:48
  • [{"MainDBName":"sampledb","EnvLabel":"BB","AnalyticsDB_ID":"DB001","EngineID":"EC002","EnvType":"bp","blockDiagramView":"VSBlockDiagram/VSBD","EnvStatus":"null","EngineName":"Testing Dummy","EnvDesc":"Dummy Place","EnvID":"EV002","itView":"VStoreIT/VStoreIT","NetCoolLog":"Yes","EnvName":"Dummy Piece","EnvLocation":"Dummy","EnvEmailID":"dummy@jimmy.com","MainDB_ID":"DB002","HealthReportDuration":"60","AnalyticsDBName":"emapit_103","NotificationSupressionInterval":"3600","TimeZone":"null"},{"MainDBName":"sampledb","EnvLabel":"CC"}] – Developer404 May 03 '11 at 06:06
  • (1) Is it the full object or you trimmed id because it's repeating the pattern? The second object is **much** shorter than the first. (2) Where do you define `index`? (3) You don't seem to have a `index++` anywhere (4) add an `alert("modify");` as the first line in your `loadmodify(...)` function so you know if it reaches there – Aleadam May 03 '11 at 06:18
  • It appears you have a stray "-" after "EnvName". – squidbe May 03 '11 at 06:22
  • I'e posted the code just for the flow and in my sourcebase it is fine with all the questions u've asked. Sorry for that. My issue is to get the jsonobj to the method loadmodify. That's it. I cannot access the content of the json passed in the parameter. – Developer404 May 03 '11 at 06:30

4 Answers4

3

A JSON object is not more than a regular javascript object. It should work if you pass it as a parameter to a function. The problem is somewhere else.

Try this version:

function retrievesrchrec(){                 
    var result = xmlHttp.responseText;
    if(result.length>1){
        var dbRecords = JSON.parse(result);
        if(dbRecords)
        {
            var index=0;
            while(dbRecords[index])
            {
                dbRecord= dbRecords[index];
                var rbutton = document.createElement("A");
                rbutton.href = loadModify(dbRecord);
                index++;
             }
        }
    }
}
function loadModify(jsonobj){
    alert(JSON.stringify(jsonobj));
    alert(jsonobj.EnvLabel);
    return "http:\/\/www.example.com";
}
Aleadam
  • 40,203
  • 9
  • 86
  • 108
1
$.ajax({
                type: "POST",
                url: "JSONService.asmx/TestJSON",
                data: '{"Name":"mohammad","Company":"Faraconesh"}',
                contentType: "application/json; charset=utf-8",
                dataType: "json"
});

and in service code

public string TestJSON(string Name,string Company)
    {
        Employee e = new Employee();

        e.Name = Name;
        e.Company = Company;}

Name & Company in my method is my jason abject value

cнŝdk
  • 31,391
  • 7
  • 56
  • 78
M.Azad
  • 3,673
  • 8
  • 47
  • 77
0

Potential problems that I can see in the code you posted:

Where is the 'index' variable from the while loop declared, initialised and incremented/updated?

Within the while loop you are trying to create an 'input' element of type 'a' and with an 'href' attribute - both of which are not valid for 'input' elements. Perhaps you really should be creating an 'a' element?

Assuming you did have an a element, doesn't the 'href' attribute have to be a string? You're assigning it to point to a function.

Your 'loadmodify()' function is declared all in lowercase but called with a capital 'M'.

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
  • I'm really sorry. The function which I have posted is just for the understanding of the flow and moreover, it is working fine in all the browsers. Now my issue is I cannot get the jsontext from the parameter. – Developer404 May 03 '11 at 06:27
  • Well I'm all for only posting cut-down versions of code, but as Felix said you need to be sure the cut-down version doesn't introduce more problems than the part you're trying to ask about. – nnnnnn May 03 '11 at 07:29
0

This part of the code is very wrong:

var rbutton = document.createElement("input");
rbutton.type = "a";
rbutton.href = function(){
    javascript:loadModify(jsonobj);
};
  1. You create an <input> element at set its type to a. This type does not exist. It has either to be text, password, hidden, button, submit or reset.
  2. It seems you think you created a link now, as you are trying to set the href attribute. Two things are wrong here:
    1. An <input> element has no href attribute.
    2. The value of an href attribute has to be a string, not a function.
  3. The label javascript: is unnecessary here.
  4. You are passing jsonobj to the function, but you never define it anywhere.

I think it is better you create a <button> and assign a click handler:

var rbutton = document.createElement("button");
rbutton.append(document.createTextNode('Click me!')); // you need some text
rbutton.onclick = function(){
    loadModify(dbRecord); // pass dbRecord here
};
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • Please.. Ignore the code. It is just for understanding the flow. It is working. I've posted only partial code. Sorry. My issue is to get the json object value in load modify method. – Developer404 May 03 '11 at 06:39
  • 3
    @Nila: Well, even if you post partial code, you have to post **correct** code. How should we know where the exact problem is otherwise? Please provide a minimal working example that reproduces your problem. – Felix Kling May 03 '11 at 06:40