[英]send arrays of data from php to javascript
My jphp.php file contains the following : 我的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);
?>
and my javascript file contains the following: 并且我的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(','));
}
});
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. 我已经用php编码了数据数据....现在我想将这两个数据数组发送到javascript .....我不知道要使用的正确命令。 I am getting confused on googling. 我对谷歌搜索感到困惑。
Please help . 请帮忙 。
JavaScript通过AJAX调用PHP,然后在得到响应时使用JSON.parse()
将JSON字符串转换为JavaScript对象。
On the client-side I would recommend using jQuery and it's $.parseJSON()
function. 在客户端,我建议使用jQuery及其$.parseJSON()
函数。
You can do the AJAX call using $.get()
, $.post()
or $.ajax()
. 您可以使用$.get()
, $.post()
或$.ajax()
进行AJAX调用。 See the documentation for their usage. 请参阅文档以了解其用法。
On the server-side, encode your array with PHP native json_encode()
function. 在服务器端,使用PHP本机json_encode()
函数对数组进行编码。
Then set the correct HTTP header (!!!) 然后设置正确的HTTP标头(!!!)
header('Content-type: application/json');
and echo the JSON encoded data =] 并回显JSON编码数据=]
Simple specific exmple using jquery: 使用jquery的简单特定示例:
The javascript page: 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;
}
});
In your php: 在您的php中:
$send_array = array(
'edge_number' => array('a','b'),
'vertex_a' => array('c','d')
);
header('Content-type: application/json');
echo json_encode($send_array);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.