开发者

How to pass json object as a parameter to another method

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 = documen开发者_开发百科t.createElement("input");
                rbutton.type = "a";
                rbutton.href = function(){
                    javascript:loadModify(jsonobj);
                };

            }
        }
    }
}
function loadmodify(jsonobj){
    alert(jsonobj);
}


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


$.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
};
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜