javascript 使用 jQuery 将 JSON 发送到服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4118226/
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-10-25 10:24:51  来源:igfitidea点击:

Send JSON to the server using jQuery

javascriptjqueryjsongetjson

提问by toomuchcs

I have the need to send an array of ID's to my server-side in the form of a JSON Object.I am using a dropdownlist where multiple values can be selected to do an action with them.

我需要以 JSON 对象的形式将一组 ID 发送到我的服务器端。我正在使用一个下拉列表,其中可以选择多个值来对它们执行操作。

To get them in an array, I've used:

为了将它们放入数组中,我使用了:

var selectedArray = [];

            var selectObj = document.getElementById('addedList');
            var i=0;
            var count=0;
            for(i=0;i<selectObj.options.length;i++){
                selectedArray[count] = selectObj.options[i].value;
                count++;
            }

Now the question is, I need to get those ID's to the server. I've always thought of sending it like a JSON object, since it has a variable amount of parameters. As far as I found out, you can convert a JS object to JSON.

现在的问题是,我需要将这些 ID 发送到服务器。我一直想像发送 JSON 对象一样发送它,因为它具有可变数量的参数。据我所知,您可以将 JS 对象转换为 JSON。

Now I do have a few questions:

现在我确实有几个问题:

Could you give me an example of how to convert it? There seem to be a million ways, one of them being JSON.stringify(jsObj);. My object would simply consist of an array of values. As far as I know this would be an example:

你能给我一个如何转换它的例子吗?似乎有一百万种方法,其中之一是JSON.stringify(jsObj);. 我的对象将简单地由一组值组成。据我所知,这将是一个例子:

{ array : ["value1","value2","value3"] }

Another question is: How can I send this using jQuery? Can I send a JSON object to the server using $.getJSON? (This uses $.GET under the hood), or do I need to use $.POST ?

另一个问题是:如何使用 jQuery 发送此信息?我可以使用 $.getJSON 将 JSON 对象发送到服务器吗?(这在幕后使用 $.GET ),还是我需要使用 $.POST ?

Now I've just been trying, but can't get it out...

现在我一直在尝试,但无法摆脱......

$.getJSON code

$.getJSON 代码

        $.getJSON("removerequest.htm",{ ids: JSON.stringify(selectedArray) }, function(data){
            $('#removerequestdiv').text('');

            $('#removerequestdiv').append('<select name="addedList">');
            for(var index in data){
                $('#removerequestdiv').append('<option value="' + data[index].id + '">' + data[index].voornaam + data[index].familienaam + '</option>');
            }
            $('#removerequestdiv').append('</select>');
        });

回答by Pointy

The $.getJSON()routine is for fetchingJSON-encoded content fromthe server. Your problem is the opposite: you want to send it tothe server.

$.getJSON()例程用于服务器获取JSON 编码的内容。你的问题正好相反:你想把它发送服务器。

You should understand the terminology here. There's no such thing really as a "JSON object" in Javascript. It's just a Javascript object of some sort, and there's nothing special about it in that sense. What you want to do is serializethe Javascript object into a single string. That string is your parameter you'll send to the server, and the server will deserializethat string back into an object (in the context of whatever language your server code is using).

你应该理解这里的术语。在 Javascript 中没有真正的“JSON 对象”这样的东西。它只是某种类型的 Javascript 对象,从这个意义上说它没有什么特别之处。您想要做的是将 Javascript 对象序列化为单个字符串。该字符串是您将发送到服务器的参数,服务器将该字符串反序列化回一个对象(在您的服务器代码使用的任何语言的上下文中)。

Thus, when you call JSON.stringify(obj), what you get is just a string. Passing such a string back to the server is no different than passing any other string; it's just a parameter. Use $.post()to post it, or you can even just stuff it into the value of a simple form input element and post a form the old-fashioned way.

因此,当您调用 时JSON.stringify(obj),您得到的只是一个字符串。将这样的字符串传递回服务器与传递任何其他字符串没有什么不同;它只是一个参数。使用$.post()后它,或者你甚至可以只是把这些东西它变成一个简单的表单输入元素的值和发布形式老式的方法。