如何使用 JSONP 下载客户端 javascript 对象?

发布于 2024-08-25 09:45:31 字数 1663 浏览 4 评论 0原文

我正在尝试将客户端 JavaScript 对象保存为本地文件。我不确定这是否可能。

基本架构是这样的:

  1. Ping 外部 API 以获取 JSON 对象
  2. 在客户端使用该对象,最终得到一个“下载我”链接
  3. 此链接将数据发送到我的服务器,服务器处理它并将其发送回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 字符串并将其序列化,这很棒。我遗漏了两件事:

实际问题

  1. 我需要做什么才能获得与在浏览器中访问页面时相同的提示下载行为
  2. (更简单)如何做我在服务器端访问发送到服务器以序列化它的 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:

  1. Ping an external API to get back a JSON object
  2. Work client-side with that object, and eventually have a "download me" link
  3. 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

  1. 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
  2. (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小ぇ时光︴ 2024-09-01 09:45:31
  1. 您需要告诉浏览器访问该页面,通常通过设置window.location
  2. 由于它是一个字符串,因此它将作为原始查询字符串的一部分发送。尝试在 $_SERVER['QUERY_STRING'] 中查找。
  1. You need to tell the browser to visit the page, usually by setting window.location.
  2. Since it's a string, it will be sent as part of the raw query string. Try looking in $_SERVER['QUERY_STRING'] for it.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文