admin管理员组

文章数量:1287523

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);
}

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);
}
Share Improve this question edited May 3, 2011 at 5:39 Developer404 asked May 3, 2011 at 5:32 Developer404Developer404 5,97217 gold badges69 silver badges104 bronze badges 10
  • similar question on SO : stackoverflow./questions/829643/… – naiquevin Commented May 3, 2011 at 5: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 Commented May 3, 2011 at 5:38
  • I've parsed and I'm iterating the json returned. Everything is done through javascript only. Please see the code – Developer404 Commented May 3, 2011 at 5:42
  • What's the output of console.log(result);? – Aleadam Commented May 3, 2011 at 5:43
  • Just Object[Object] for printing the jsonobj. When I tried to retrieve the content jsonobj["ID"]. I got undefined as the value. – Developer404 Commented May 3, 2011 at 5:45
 |  Show 5 more ments

4 Answers 4

Reset to default 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.";
}
$.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

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'.

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
};

本文标签: javascriptHow to pass json object as a parameter to another methodStack Overflow