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
4 Answers
Reset to default 3A 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);
};
- You create an
<input>
element at set itstype
toa
. This type does not exist. It has either to betext
,password
, hidden,button
,submit
orreset
. - It seems you think you created a link now, as you are trying to set the
href
attribute. Two things are wrong here:- An
<input>
element has nohref
attribute. - The value of an
href
attribute has to be a string, not a function.
- An
- The label
javascript:
is unnecessary here. - 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
版权声明:本文标题:javascript - How to pass json object as a parameter to another method - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741310213a2371608.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论