I've got a php script with collects data from a server and displays it in an array and after that as a json with the function.
我有一个php脚本,它从服务器收集数据并将其显示在一个数组中,然后以json的形式显示函数。
echo json_encode($result);
Now I want to access that array with my javascript and display it. It should be saved in a var as an array so it should look like:
现在我想用javascript访问这个数组并显示它。它应该作为数组保存在var中,因此应该如下:
data = [ "xxxx" , "ssss",];
But I guess I can simply put in my function which gets the array data instead so it'd be:
但是我想我可以简单地输入我的函数来得到数组数据,结果是:
data = myfunction ;
What I've tried so far:
到目前为止我所尝试的:
function reqListener () {
console.log(this.responseText);
}
var oReq = new XMLHttpRequest(); //New request object
oReq.onload = function() {
};
oReq.open("get", "http://myserver.com/myscript.php", true);
oReq.send();
and
和
function getdata(url) {
jQuery.ajax(
{
type: "GET",
url: "http://myserver.com/myscript.php/",
dataType: "text",
success: function (response) {
var JSONArray = jQuery.parseJSON(response);
connsole.log(JSONArray);
}
});
}
But none seems to work and I get displayed 'undefined' instead of my arrays. Would be really great if somebody has some ideas on that and can help me out.
但是没有一个看起来有效,我被显示为“未定义”而不是数组。如果有人对此有一些想法并能帮助我,那就太好了。
Edit: Since we are getting nowhere here's my php code:
编辑:因为这里没有我的php代码:
<?php
error_reporting(0);
$html = file_get_contents("url here");
$dom = new DOMDocument();
$dom->loadHTML($html);
$tbodyRows = $dom->getElementsByTagName( 'tbody' )
->item( 0 ) // grab first tbody
->getElementsByTagName( 'tr' );
$result = array();
foreach( $tbodyRows as $tbodyRow )
{
$result[] = $tbodyRow->getElementsByTagName( 'td' )
->item( 2 ) // grab 3rd column
->nodeValue;
}
echo json_encode($result);
?>
2 个解决方案
#1
1
Try this code:
试试这段代码:
function getdata(url) {
console.log('Started');
jQuery.ajax({
type: "GET",
url: "http://myserver.com/myscript.php",
dataType: "text",
error: function (xhr) {
console.log('Error',xhr.status);
},
success: function (response) {
console.log('Success',response);
}
});
}
Open the browser's console, and let me know about its contents. If you don't see Error
or Success
, your code isn't actually executing
打开浏览器的控制台,告诉我它的内容。如果您没有看到错误或成功,那么您的代码实际上并没有执行。
#2
1
I have done a similar thing earlier. I will describe it and I wish it will help you.
我之前也做过类似的事情。我将描述它,我希望它能帮助你。
In the following code (get_categories.php), I am retrieving data from the database and add them to an array. Then return it by encoding as a JSON.
在下面的代码中(get_category .php),我正在从数据库中检索数据并将它们添加到一个数组中。然后通过JSON编码返回。
$sql = "SELECT category_name FROM category;";
$dataArray = [];
$result = $connection->query($sql);
if ($result) {
while ($row = $result->fetch_assoc()) {
$dataArray[] = $row;
}
echo json_encode($dataArray);
}
Then in my Javascript code, I can get the data as follows.
然后在我的Javascript代码中,我可以得到如下数据。
$.ajax({
url: "/get_categories.php",
type: "GET",
dataType: "json",
success: function (categories) {
for (var i = 0; i < categories.length; i++) {
console.log(categories[i]);
}
},
error: function (jqXHR, textStatus, errorThrown) {
// Error handling code
}
});
#1
1
Try this code:
试试这段代码:
function getdata(url) {
console.log('Started');
jQuery.ajax({
type: "GET",
url: "http://myserver.com/myscript.php",
dataType: "text",
error: function (xhr) {
console.log('Error',xhr.status);
},
success: function (response) {
console.log('Success',response);
}
});
}
Open the browser's console, and let me know about its contents. If you don't see Error
or Success
, your code isn't actually executing
打开浏览器的控制台,告诉我它的内容。如果您没有看到错误或成功,那么您的代码实际上并没有执行。
#2
1
I have done a similar thing earlier. I will describe it and I wish it will help you.
我之前也做过类似的事情。我将描述它,我希望它能帮助你。
In the following code (get_categories.php), I am retrieving data from the database and add them to an array. Then return it by encoding as a JSON.
在下面的代码中(get_category .php),我正在从数据库中检索数据并将它们添加到一个数组中。然后通过JSON编码返回。
$sql = "SELECT category_name FROM category;";
$dataArray = [];
$result = $connection->query($sql);
if ($result) {
while ($row = $result->fetch_assoc()) {
$dataArray[] = $row;
}
echo json_encode($dataArray);
}
Then in my Javascript code, I can get the data as follows.
然后在我的Javascript代码中,我可以得到如下数据。
$.ajax({
url: "/get_categories.php",
type: "GET",
dataType: "json",
success: function (categories) {
for (var i = 0; i < categories.length; i++) {
console.log(categories[i]);
}
},
error: function (jqXHR, textStatus, errorThrown) {
// Error handling code
}
});