将json数据发布到php并回显结果

问题描述 投票:0回答:2

我是一个正在努力学习 php 和 javascript 的人,一直在疯狂地寻找解决方案,但没有成功。我正在尝试使用 php 将 json 对象/字符串从一个页面发送到另一个页面,然后在该新页面中回显结果(最终使用 tcppdf 生成 pdf)。所以基本上一些 JavaScript 会在一个页面中生成一个对象,

pageStructure
,然后我将其字符串化:

var jsonString = JSON.stringify(pageStructure);
alert(jsonString);`

警报弹出正常。

我现在想将其发送(发布)到另一个 php 文件

getdata.php
,然后使用它来构建 pdf。

我尝试过使用表单发布,但使用

jsonString
更新表单中输入的值不起作用。

**添加 - 在这里解释我的问题 我创建了一个表格如下:

<form action="getdata.php" method="post">
  <textarea type="hidden" id="printMatter" name="printMatter" value=""></textarea>          
  <button type="submit"><span class="glyphicon glyphicon-eye-open" ></span></button>
</form>

在构造

jsonString
后,我有一些代码将
textarea
的值设置为该值:

document.getElementById('printMatter').value = jsonString;
alert(document.getElementById('printMatter').value);

提交按钮激活打开 getdata.php 页面的表单,但我注意到两件事:

(1) 发送 jsonString 字符串之前,每个引号 (") 之前都充满了转义符 ()。

(2) 当 getdata.php 打开时,回显的 jsonString 已更改为不包含 \s,而是包含 json 字符串中对象的值('value')之一(一段包含大量 \s 的 svg 代码) -例如(被截断,因为该值是一个非常长的 svg 字符串,但这给出了想法):

{"type":"chartSVG","value":"<g transform=\"translate(168.33333333333334,75)\" class=\"arc\">...

已更改为整数 - 例如:

{"type":"chartSVG","value":"12"}

我不明白这种情况是如何或为何发生的,以及如何在表单发布后维护完整的 svg 代码。

**

我尝试使用 jquery/ajax 如下:

$.ajax({
  url: 'getdata.php',
  type: 'post',
  data: {printMatter: jsonString},
  success: function(){
    alert('it worked');
  },
  error: function(){
    alert('it failed')}
})

我收到了成功响应,但最终出现在同一页面上,而不是获取新的 php 文件来回显正在发送的内容!

php 脚本包含以下内容:

<?php
  echo $_POST['printMatter'];
?>

但这行不通。也没有尝试向 php 页面添加标头(例如

header('Content: application/json')
。我最终停留在原来的页面上。我如何才能将其留在新页面(getdata.php)上并带有 json 字符串的回显?

任何人都可以解释我做错了什么或者我如何才能得到我想要的东西?

非常感谢。

**添加 这表明我如何获得

jsonString
对象:

 function item(type,value) {
  this.type = type;
  this.value = value;
 }

for (i=0;i<thePage[0].length;i++) {
  pageid = thePage[0][i].id;
  var entry = new item("page",pageid);
  pageStructure.push(entry);
}

var jsonString = JSON.stringify(pageStructure);

所以我最终在

jsonString
中列出了一系列页面。

javascript php ajax json post
2个回答
1
投票

尝试将 $_POST 更改为 $_GET,因为您的 AJAX 请求执行的是 HTTP GET 而不是 HTTP POST。


0
投票

您正在使用 AJAX。本质上,AJAX 不会刷新页面,例如,如果您这样做:

$.ajax({
  url: 'getdata.php',
  type: 'post',
  data: {printMatter: jsonString},
  success: function(data){
    alert('it worked');
    alert('You sent this json string: ' + data);
  },
  error: function(){
    alert('it failed')}
});

另请注意,我将您的

type
'get'
更改为
'post'
...此处设置的类型将部分确定您可以在哪里访问要发送的数据...如果您将其设置为 getdata 中的 get .php 你需要使用
$_GET
,如果你将其设置为
post
那么你应该使用
$_POST

现在,如果您确实想要像您暗示的那样刷新整个页面,那么您需要以另一种方式执行此操作。我不能说你会如何去做,因为你在发送之前没有提供足够的关于如何获得你的

jsonString
的想法。

更新

这不会让我停留在我想要的页面上。我不想刷新页面,只是重定向到接收发布的 json 数据的新页面。

这本质上是一个页面“刷新”,尽管“刷新”可能会误导您,因为它可能意味着重新加载当前的 URL。我所说的刷新的意思是加载一个全新的页面。这本质上就是您所要求的。有一个有几种方法可以解决这个问题...

如果您的数据非常短并且不会违反网络服务器上 URI 的最大长度,那么您可以使用

window.location
:

// send it as json like you are currently trying to do
window.location = 'getdata.php?printMatter=' + encodeURIComponent(jsonString);

// OR send it with typical url-encoded data and dont use JSON
window.location = 'getdata.php?' + $.serialize(pageStructure);

在这种情况下,您将使用

$_GET['printMatter']
来访问数据,而不是
$_POST['printMatter']

如果数据有可能产生长字符串,那么您将需要 POST 它。这变得有点棘手,因为如果我们想要 POST,我们必须使用表单。使用 JSON 和 jQuery 非常简单:

var form = '<form action="getdata.php" method="post">'
           +    '<input type="hidden" name="printMatter" value="%value%" />'
           + '</form>';

form.replace('$value%', jsonString);

// if you have any visual styles on form that might then you may 
// need to also position this off screen with something like 
// left: -2000em or what have you
$(form).css({position: 'absolute'}) 
  .appendTo('body')
  .submit();

如果我们只想将其作为普通表单数据发送,那么它会变得更加复杂,因为我们需要递归循环

pageStructure
并使用正确的
name
属性创建输入元素...我不会走那条路。

所以最后的方法(但我不认为它会起作用,因为看起来你正在尝试生成一个文件并让浏览器下载它)是通过 AJAX 发送它并让 ajax 返回下一个网址至:

JS

$.ajax({
  url: 'getdata.php',
  type: 'post',
  data: {printMatter: jsonString},
  type: 'json',
  success: function(data){
    window.location = data.redirectUrl;
  },
  error: function(){
    alert('it failed')}
});

获取数据.php

// do something with the $_POST['printMatter'] data and then...

$response = array(
  'redirectUrl' =>$theUrlToGoTo
);

header('Content-Type: application/json');
print json_encode($response);
© www.soinside.com 2019 - 2024. All rights reserved.