简体   繁体   English

Json_encoding Ajax请求中的Php变量

[英]Json_encoding a Php Variable in an Ajax Request

Currently I am using a jQuery Ajax asynchronous function to request a PHP page multiple times until a large number of spreadsheet rows are processed. 目前,我正在使用jQuery Ajax异步函数多次请求PHP页面,直到处理了大量电子表格行。 Right now I am using the following code to set the variables to be passed to the requested page, I am not sure if this is the proper way to do it or not. 现在,我正在使用以下代码来设置要传递到请求页面的变量,我不确定这是否是正确的方法。 PHP: PHP:

if($_SERVER['REQUEST_METHOD'] == 'POST') {
    $new_spreadsheet = nl2br($_POST['spreadsheet']);
    $new_spreadsheet = explode('<br />', $new_spreadsheet);
    array_shift($new_spreadsheet);
    $new_spreadsheet = array_values($new_spreadsheet);
    $new_spreadsheet = json_encode($new_spreadsheet);
    echo var_dump($new_spreadsheet);
}

JavaScript/PHP: JavaScript / PHP:

var done = false,
offset = 0,
limit = 20,
rankings_abv_twenty = 0,
sum = 0,
num_count = 0,
websites = 1
<?php if($_SERVER['REQUEST_METHOD'] == 'POST') {
    echo ', spreadsheet = '.$new_spreadsheet;
}?>;

On the requested PHP page (which happens to be Object-Oriented PHP), I have the following code to slice off 20 rows to process on the given request. 在所请求的PHP页面(恰好是面向对象的PHP)上,我有以下代码可以分割20行以处理给定请求。 Please not the PHP is a full working class, I just did not include all the PHP for the sake of post length. 请注意,PHP不是一个完整的工作类,出于篇幅太长,我只是不包括所有PHP。 Also note, I have another page which calls the class, which passes the spreadsheet variable to the constructor of the PHP by reference as $_POST['spreadsheet'], so I know I have the right value as the spreadsheet. 还要注意,我还有另一个页面调用该类,该页面通过将电子表格变量以$ _POST ['spreadsheet']的形式传递给PHP的构造函数,因此我知道我拥有与电子表格相同的值。 PHP: PHP:

$this->offset = $offset;
$this->limit = $limit;
$this->spreadsheet = json_decode($spreadsheet);

Here is the PHP line which slices off the rows: 这是将行切成段的PHP行:

$this->rows = array_slice($this->spreadsheet, $this->offset, $this->limit);

For some reason my code is not working properly and is giving me the following errors relating to the above code: 由于某种原因,我的代码无法正常工作,并且给我以下与上述代码有关的错误:

PHP Warning:  json_decode() expects parameter 1 to be string, array given
PHP Warning:  array_slice() expects parameter 1 to be array, null given

I think it might have something to do with how my string is getting passed the requested PHP. 我认为这可能与我的字符串如何传递给请求的PHP有关。 Also, just as a further note, when I use var_dump() on my spreadsheet variable before it is passed to the requested PHP, I get it outputted as a string like so: 另外,作为进一步说明,当我在电子表格变量上使用var_dump()并将其传递给请求的PHP之前,我将其输出为字符串,如下所示:

string(7560) "String content goes here"

On the first look, instead of echoing the json_encode, you used var_dump. 乍一看,您没有使用json_encode,而是使用var_dump。

And when manually modifying the json encoded string make sure to use " around the vars. 并且当手动修改json编码的字符串时,请确保在变量周围使用“”。

And this: 和这个:

  websites = 1
  <?php if($_SERVER['REQUEST_METHOD'] == 'POST') {
    echo ', spreadsheet = '.$new_spreadsheet;
   }?>;

Will result in: 将导致:

  websites = 1, spreadsheet = "{"1":"foo","2":"bar","3":"baz","4":"blong"}";

Wrap the line in ' if that's what you expect it.. 如果希望的话,将行换成'。

like this: 像这样:

 echo ", spreadsheet = '".$new_spreadsheet."'";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM