Javascript 如何将json对象作为参数传递给另一个方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5865442/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 19:15:36  来源:igfitidea点击:

How to pass json object as a parameter to another method

javascriptjsonjsp

提问by Developer404

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:

我正在使用两个 jsp,一个用于搜索记录,另一个用于更新数据库的记录。搜索查询将返回一个 json 对象,其中包含所有搜索到的记录,我正在将行动态生成到表中以供显示。为了更新记录,我在每一行都保留了链接。在这里,我必须将 json 对象传递给更新的 jsp 文件以加载值。所以,我只是传递了 json 对象。但是,我无法处理 json 对象。帮帮我。谢谢。请在下面找到代码:

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

回答by Aleadam

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.

一个 JSON 对象不过是一个普通的 javascript 对象。如果您将它作为参数传递给函数,它应该可以工作。问题出在其他地方。

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

回答by M.Azad

$.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& Companyin my method is my jason abject value

Name&Company在我的方法中是我的杰森卑鄙的价值

回答by nnnnnn

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?

while 循环中的“index”变量在哪里声明、初始化和递增/更新?

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?

在 while 循环中,您尝试创建一个类型为 'a' 并具有 'href' 属性的 'input' 元素 - 这两个元素都对 'input' 元素无效。也许您真的应该创建一个“a”元素?

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.

假设你确实有一个 a 元素,'href' 属性不是必须是一个字符串吗?您将其分配为指向一个函数。

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

您的“loadmodify()”函数全部以小写形式声明,但调用时使用大写字母“M”。

回答by Felix Kling

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 typeto a. This type does not exist. It has either to be text, password, hidden, button, submitor reset.
  2. It seems you think you created a link now, as you are trying to set the hrefattribute. Two things are wrong here:
    1. An <input>element has no hrefattribute.
    2. The value of an hrefattribute has to be a string, not a function.
  3. The label javascript:is unnecessary here.
  4. You are passing jsonobjto the function, but you never define it anywhere.
  1. <input>在将其设置type为 时创建一个元素a。这种类型不存在。它必须是textpassword、 隐藏buttonsubmitreset
  2. 您似乎认为您现在创建了一个链接,因为您正在尝试设置该href属性。这里有两件事是错误的:
    1. 一个<input>元素没有href属性。
    2. href属性的值必须是字符串,而不是函数。
  3. javascript:这里不需要标签。
  4. 您正在传递jsonobj给函数,但您从未在任何地方定义它。

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

我认为最好创建一个<button>并分配一个click处理程序:

var rbutton = document.createElement("button");
rbutton.append(document.createTextNode('Click me!')); // you need some text
rbutton.onclick = function(){
    loadModify(dbRecord); // pass dbRecord here
};