Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
604 views
in Technique[技术] by (71.8m points)

send arrays of data from php to javascript

My jphp.php file contains the following :

<?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);

?>

and my javascript file contains the following:

<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(','));
  }
});

I have encode the data data in php .... now i want to send those two arrays of data to javascript..... i don't know the proper commands to use. I am getting confused on googling.

Please help .

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The JavaScript calls the PHP via AJAX, and then when it gets the respone it uses JSON.parse() to turn the JSON string into JavaScript objects.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...