javascript 编码字符串javascript,以便它可以传输到服务器

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

Encode string javascript so that it can be transmitted to a server

javascriptjsonjqgrid

提问by London

I'm trying to send json string in the get request to the server, here is how it looks before encoding :

我正在尝试将 get 请求中的 json 字符串发送到服务器,这是编码前的样子:

filters={"groupOp":"AND","rules":[{"field":"countrycode","op":"eq","data":"ARG"}]}

Naturally I end up with null pointer when trying to get this json string, then I googled this encodeURIComponent and it partially encodes this string like this :

自然地,当我试图获取这个 json 字符串时,我最终得到了空指针,然后我用谷歌搜索了这个 encodeURIComponent 并且它部分地编码了这个字符串,如下所示:

filters={"groupOp"%3A"AND"%2C"rules"%3A[{"field"%3A"countrycode"%2C"op"%3A"eq"%2C"data"%3A"ARG"}]}

But this is how it supposed to be in order to work :

但这就是它应该如何工作:

filters=%7B%22groupOp%22%3A%22AND%22%2C%22rules%22%3A%5B%7B%22field%22%3A%22countrycode%22%2C%22op%22%3A%22eq%22%2C%22data%22%3A%22ARG%22%7D%5D%7D

How do I get this, entirely encoded string so I can read it at server side properly ?

我如何获得这个完全编码的字符串,以便我可以在服务器端正确读取它?

Reason why I used get instead of post

我使用 get 而不是 post 的原因

I'm sending this filter(json) content to the server side, web service gets data from the database and returns pdf document.

我将此过滤器(json)内容发送到服务器端,Web 服务从数据库中获取数据并返回 pdf 文档。

Using post, I'm able to send correct data and the response is successfully displayed in my firebug console. But I need to return pdf doc to override the current page or open new window/tab and return in that one.

使用 post,我可以发送正确的数据,并且响应成功显示在我的萤火虫控制台中。但我需要返回 pdf doc 以覆盖当前页面或打开新窗口/选项卡并返回该页面。

采纳答案by Cheeso

I think you're overworking this problem. Or encoding too many times. Or something. You've got a JSON string, and you are trying to JSON encode it. That seems...unhelpful.

我认为你在这个问题上工作过度了。或者编码次数过多。或者其他的东西。您有一个 JSON 字符串,并且您正在尝试对其进行 JSON 编码。这似乎……没有帮助。

A better approach might be to produce a Javascript object, then JSON.Stringify that, and then transmit it as a parameter.

更好的方法可能是生成一个 Javascript 对象,然后 JSON.Stringify that,然后将其作为参数传输。

var thing = {
  groupOp : "AND",
  rules : [
    { field : "countrycode", op : "eq", data : "ARG" },
      ...
  ],
    ...
}; 
var stringrep = JSON.stringify(thing);
// post via jQuery
$.ajax({
  type: 'POST',
  url: url,
  data: stringrep,
  dataType: 'json'
  success: function() { ... },
});

Normally for a JSON stringified message to or from the server, you'd want to use HTTP POST. HTTP GET puts all "parameters" in the URL; there is no message body. In contrast, HTTP POST allows you to attach a message body to the HTTP message, which can be "anything". With that approach, you don't need to url-encode the quotes and spaces; the JSON message just gets transmitted as the message body of the HTTP message.

通常对于传入或传出服务器的 JSON 字符串化消息,您需要使用 HTTP POST。HTTP GET 将所有“参数”放在 URL 中;没有消息正文。相比之下,HTTP POST 允许您将消息正文附加到 HTTP 消息,它可以是“任何东西”。使用这种方法,您不需要对引号和空格进行 url 编码;JSON 消息只是作为 HTTP 消息的消息正文传输。

HTTP POST is the way applications upload images, or transmit XML documents, and so on. Anything complex gets transmitted via POST.

HTTP POST 是应用程序上传图像或传输 XML 文档等的方式。任何复杂的东西都通过 POST 传输。

回答by rid

var filtersParameter = 'filters=' + encodeURI(JSON.stringify(filters));

回答by Anshuman

var filtersParameter = 'filters=' + atob(JSON.stringify(filters));

Note: atob()method uses base64algorithm to encode the data. This encoded data can be easily passed to a server where it can be decoded using corresponding decoding methods (in python base64.b64decode(encoded_string)is used).

注意:atob()方法使用base64算法对数据进行编码。这些编码数据可以很容易地传递到服务器,在那里可以使用相应的解码方法(在 pythonbase64.b64decode(encoded_string)中使用)进行解码。