如何使用 JSONP 下载客户端 javascript 对象?
我正在尝试将客户端 JavaScript 对象保存为本地文件。我不确定这是否可能。
基本架构是这样的:
- Ping 外部 API 以获取 JSON 对象
- 在客户端使用该对象,最终得到一个“下载我”链接
- 此链接将数据发送到我的服务器,服务器处理它并将其发送回mime 类型
application/json
,它(应该)提示用户在本地下载文件。
现在这是我的作品:
服务器端代码
<?php
$data = array('zero', 'one', 'two', 'testing the encoding');
$json = json_encode($data);
//$json = json_encode($_GET['']); //eventually I'll encode their data, but I'm testing
header("Content-type: application/json");
header('Content-Disposition: attachment; filename="backup.json"');
echo $_GET['callback'] . ' (' . $json . ');';
?>
相关客户端代码
$("#download").click(function(){
var json = JSON.stringify(collection); //serializes their object
$.ajax({
type: "GET",
url: "http://www.myURL.com/api.php?callback=?", //this is the above script
dataType: "jsonp",
contentType: 'jsonp',
data: json,
success: function(data){
console.log( "Data Received: " + data[3] );
}
});
return false;
});
现在,当我使用 Firefox 访问 api.php
站点时,它会提示下载 download.json
并按照预期生成此文本文件:
(["zero","one","two","testing the encoding"]);
当我单击 #download
运行 AJAX 调用时,它会登录 Firebug
Data Received: testing the encoding
,这几乎是我所期望的。我正在接收 JSON 字符串并将其序列化,这很棒。我遗漏了两件事:
实际问题
- 我需要做什么才能获得与在浏览器中访问页面时相同的提示下载行为
- (更简单)如何做我在服务器端访问发送到服务器以序列化它的 json 对象?我不知道 GET 数组中的索引是什么(我知道这很愚蠢,但我几乎尝试了所有方法)
I'm trying to get client-side javascript objects saved as a file locally. I'm not sure if this is possible.
The basic architecture is this:
- Ping an external API to get back a JSON object
- Work client-side with that object, and eventually have a "download me" link
- This link sends the data to my server, which processes it and sends it back with a mime type
application/json
, which (should) prompt the user to download the file locally.
Right now here are my pieces:
Server Side Code
<?php
$data = array('zero', 'one', 'two', 'testing the encoding');
$json = json_encode($data);
//$json = json_encode($_GET['']); //eventually I'll encode their data, but I'm testing
header("Content-type: application/json");
header('Content-Disposition: attachment; filename="backup.json"');
echo $_GET['callback'] . ' (' . $json . ');';
?>
Relevant Client Side Code
$("#download").click(function(){
var json = JSON.stringify(collection); //serializes their object
$.ajax({
type: "GET",
url: "http://www.myURL.com/api.php?callback=?", //this is the above script
dataType: "jsonp",
contentType: 'jsonp',
data: json,
success: function(data){
console.log( "Data Received: " + data[3] );
}
});
return false;
});
Right now when I visit the api.php
site with Firefox, it prompts a download of download.json
and that results in this text file, as expected:
(["zero","one","two","testing the encoding"]);
And when I click #download
to run the AJAX call, it logs in Firebug
Data Received: testing the encoding
which is almost what I'd expect. I'm receiving the JSON string and serializing it, which is great. I'm missing two things:
The Actual Questions
- What do I need to do to get the same prompt-to-download behavior that I get when I visit the page in a browser
- (much simpler) How do I access, server-side, the json object being sent to the server to serialize it? I don't know what index it is in the GET array (silly, I know, but I've tried almost everything)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
window.location
。$_SERVER['QUERY_STRING']
中查找。window.location
.$_SERVER['QUERY_STRING']
for it.