-1

我正在尝试通过 ajax 请求将一些值从 javascript 传递到 php 文件,并将每个结果(来自 for 循环)存储到使用 php 的数组中。

queryData={"data":{"data_string":{"data":"medicine","default_field":"Content"}}}
testArgument=0;

$.ajax({
    url:"test/queryManipulate.php",
    type: 'POST',
    datatype: 'json',
    data: {field : queryData, start : testArgument},
    success:function(jsonQuery)
    {
       alert(jsonQuery);
    }
});


<?php

    $i=0;
for ($from = 0; $from <= 50; $from+=10)
{
$object=json_decode($_POST["field"]); 
$object->from=$from; 
$object=json_encode($object);
$ch = curl_init("http://localhost:9200/algotree//_search");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $object);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$test= curl_exec($ch);
curl_close ($ch);
$newArray[$i]= $test;
$i++;
echo $newArray[2];
}
?>

我的 for 循环不起作用。我是否以正确的方式使用它,以及如何将每个结果存储到 php 中的数组中?

4

2 回答 2

0

在 javascript 中,在转换为 json 字符串之前,我们必须使用数组和对象来格式化数据,例如,

data_string  = new Object();
data = new Array();

data_string[data] = "medicine";
data_string[default_field] = "Content";

data.push(data_string);

然后将数据编码为 json ,结果会像

{"data":{"data_string":{"data":"medicine","default_field":"Content"}}}

然后通过ajax传递给php文件,然后使用解码json

json_encode(data);在php中

然后做进一步的处理..

于 2012-07-17T05:58:44.810 回答
0

第一个循环应该是

    <?php
        for($from = $_POST["start"]; $from<50;$from+=10)
        {
             // other code
        }

注意 $from += 10 不是 $from+10。

于 2012-07-17T04:25:51.583 回答