【问题标题】:send arrays of data from php to javascript将数据数组从 php 发送到 javascript
【发布时间】:2010-11-27 08:19:20
【问题描述】:

我的 jphp.php 文件包含以下内容:

<?php

$send_array = array();
$edge_number = array('a','b');

$vertex_a = array('c','d');

$send_array[0] = $edge_number;
$send_array[1] = $vertex_a;

echo json_encode($send_array);

?>

我的 javascript 文件包含以下内容:

<html>
<head>
<script language="javascript">
function postRequest(strURL)
{
    var xmlHttp;
    if(window.XMLHttpRequest)
    { // For Mozilla, Safari, ...
        var xmlHttp = new XMLHttpRequest();
    }
    else if(window.ActiveXObject)
    { // For Internet Explorer
        var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlHttp.open('GET', 'jphp.php', true);
    xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlHttp.onreadystatechange = function()
    {
        if (xmlHttp.readyState == 4)
        {
    var obj = jQuery.parseJSON('{"name":"John"}');
alert( obj.name === "John" );       updatepage(xmlHttp.responseText);
        }
    }
    xmlHttp.send('jphp.php');
}

function updatepage(str)
{
    document.write(str);
}



var vertex_a = new Array();
var edge_number = new Array();
var rec_array = new Array();
rec_array = {"edge_number", "vertex_a"};
//rec_array[1] = names;
for(var i=0;i<1;i++)
{
    document.write(rec_array[i]);
}
$.ajax({
  url: 'jphp.php'
  type: 'post', // post or get method
  data: {}, // if you need to pass post/get parameterds you can encode them here in JSON format
  dataType: 'json', // the data type you want returned... we will use json
  success: function(responseData) {
    alert('edge_number='+responseData[0].join(','));
    alert('vertex_a='+responseData[1].join(','));
  }
});

我已经在 php 中对数据数据进行了编码……现在我想将这两个数据数组发送到 javascript……我不知道要使用的正确命令。我对谷歌搜索感到困惑。

请帮忙。

【问题讨论】:

    标签: php javascript html ajax json


    【解决方案1】:

    使用jquery的简单具体例子:

    javascript 页面:

    $.ajax({
      url: 'url/of/page.php'
      type: 'post', // post or get method
      data: {}, // if you need to pass post/get parameterds you can encode them here in JSON format
      dataType: 'json', // the data type you want returned... we will use json
      success: function(responseData) {
        var edge_number = responseData.edge_number;
        var vertex_a= responseData.vertex_a;
        var rec_array = responseData;
      }
    });
    

    在你的 php 中:

    $send_array = array(
      'edge_number' => array('a','b'),
      'vertex_a' => array('c','d')
    ); 
    
    header('Content-type: application/json');
    echo json_encode($send_array);
    

    【讨论】:

    • 再次感谢神童!让我试试这个。
    • 我刚刚调整为使用命名键而不是数字索引。如果您描述了在 js 中实际需要使用什么格式,那会容易得多。因为您可以直接在 php 中以这种方式对其进行格式化并将其作为 json 发送回。
    • 我的 php 脚本和你的一样 ....for javascript ....wait ....请 ...m 运行它
    • 我实际上在客户端解密编码部分时遇到了问题....
    • 实际上我认为 m AJAX 部分不起作用...你能告诉我 ajax 部分的问题吗...,我希望将 2 个数组的数据值打印在屏幕上.
    【解决方案2】:

    JavaScript 通过 AJAX 调用 PHP,然后当它得到响应时,它使用JSON.parse() 将 JSON 字符串转换为 JavaScript 对象。

    【讨论】:

    • 你能告诉我具体的命令吗....我已经尝试了 2-3 个小时....拜托....特别是 javascript 部分!
    • 你能告诉我一件事吗:我怎样才能在一个数组中构建 2 个数组....各个数组的元素必须通过 php 接收。
    【解决方案3】:

    在客户端,我建议使用 jQuery,它是 $.parseJSON() 函数。
    您可以使用$.get()$.post()$.ajax() 进行AJAX 调用。有关它们的用法,请参阅文档。

    在服务器端,使用 PHP 原生 json_encode() 函数对数组进行编码。
    然后设置正确的 HTTP 标头 (!!!)

    header('Content-type: application/json');

    并回显 JSON 编码的数据 =]

    【讨论】:

    • 你能告诉我一件事吗:我怎样才能在一个数组中构建 2 个数组....各个数组的元素必须通过 php 接收
    • ofc, $container=array($firstarray,$secondarray); 无论您的数组有多少维(“深度级别”)。 json_encode 会为你做辛苦的工作。
    猜你喜欢
    • 2012-12-14
    • 2017-06-17
    • 1970-01-01
    • 2013-03-29
    • 2015-11-03
    • 1970-01-01
    • 1970-01-01
    • 2014-01-08
    相关资源
    最近更新 更多