javascript 如何打开弹出窗口并向其发送 JSON 对象

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

How to open a popup and send a JSON object to it

javascriptjquery

提问by Morteza Ziyae

I want to send a JSONobject to a PHPfile while my script opens that PHP file in a new popupwindow. Is it possible to send it via POST method (by jQuery or without it)?

我想将JSON对象发送到PHP文件,同时我的脚本在新的弹出窗口中打开该 PHP 文件。是否可以通过 POST 方法(通过 jQuery 或不通过它)发送它?

If not, how do I convert JSON to a URL encodedstring? Is there any JavaScript function?

如果没有,如何将 JSON 转换为URL 编码的字符串?有没有 JavaScript 函数?

回答by Dr.Molle

You may create a form (on the fly) with an input (where you fill the value with the JSON) and a target-attribute regarding to name of the popup (second parameter of window.open()).

您可以使用输入(在其中使用 JSON 填充值)和关于弹出窗口名称的目标属性( 的第二个参数window.open())创建一个表单(即时)。

Then send this form.

然后发送此表格。

回答by Aaron Digulla

  1. Open the popup: var popup = window.open(...)
  2. Assign the json object to a new variable in the new window: popup.json = ...
  3. Use the variable jsonin your popup (it will be accessible as window.jsonor just jsonfrom JavaScript code running in the popup).
  1. 打开弹窗: var popup = window.open(...)
  2. 在新窗口中将 json 对象分配给一个新变量: popup.json = ...
  3. json在弹出窗口中使用该变量(它可以作为window.json或仅从json弹出窗口中运行的 JavaScript 代码访问)。

回答by Peter Bridger

There's a JSON encoder/decoderthat looks like it would do the job. You could call this to encode your object before adding it to your querystring.

有一个JSON 编码器/解码器看起来可以完成这项工作。在将对象添加到查询字符串之前,您可以调用它来对对象进行编码。

Example

例子

alert(JSON.encode([0,1,false,true,null,[2,3],{"some":"value"}]));
// [0,1,false,true,null,[2,3],{"some":"value"}]

alert(JSON.decode('[0,1,false,true,null,[2,3],{"some":"value"}]'))
// 0,1,false,true,,2,3,[object Object]

回答by Baz1nga

You can use the Ajax APIto do so... In the Ajax API, you can specify the data property and set it a JSON, and it will send the data to the server based on the type you have set, that is, GETor POST.

您可以使用Ajax API来执行此操作... 在 Ajax API 中,您可以指定 data 属性并将其设置为JSON,它会根据您设置的类型将数据发送到服务器,即GETPOST

For example:

例如:

$.ajax(
{
    url: url,                     // Your post URL
    type:"POST",                  // or GET
    data: {a:1}                   // In your case, your JSON object
    success:function(response){}, // Function that will be called when your posted URL responds
    crossDomain: true,            // If it's a cross-domain request
    dataType: "json"              // Response datatype: JSON, text, HTML, XML, etc.
}
);

One thing to note is that if your response needs to be processed, you need to get around the same origin policy set by the browser. Please read about the same. You could use something called JSONP. It's part of the Ajax API.

需要注意的一件事是,如果您的响应需要处理,您需要绕过浏览器设置的同源策略。请阅读相同的内容。你可以使用一种叫做JSONP 的东西。它是 Ajax API 的一部分。

I hope this is what you want.

我希望这就是你想要的。