Json用PHP mysql和Jquery

I have a strange problem..

When i try to access my json code with the corresponding number [0], [1] etc.. I just get the first character of the object.

First of my code:

test2.php

if(isset($_POST['getCustomersArray'])){
$runQuery = mysql_query($Query) or
        die("SQL: $Query)<br />".mysql_error()); 
$numrows = mysql_num_rows($runQuery); 
$array = array(array());

for($i = 0;$i <= 2; $i++){
    $row = mysql_fetch_array($runQuery);
    $array[$i]['namn'] = $row['fornamn'];

}
    print json_encode($array);
}

scriptfile.js

$.ajax({
    type:"POST",
    url: "test2.php",
    data: "getCustomersArray=true",
    datatype: "JSON",
    cache: false,
    success: function(json) {
            console.log(json[0]);
     }
});

The result (from console.log(json[0])):

[

The result from just console.log(json):

[{"namn":"the first name"},{"namn":"The secound name"},{"namn":"the third name"}]

Im not sure why the squarebrackets are there but maybe they should be?

Ive been fuzzing with this problem for a while now and im sure its something stupid. Please help.

3个回答

You have an incorrect option in the AJAX settings,

datatype: "json",

It should be:

dataType: "json",
douzhe9075
douzhe9075 too long
接近 8 年之前 回复
dphfwzn8269
dphfwzn8269 啊哈,我认为jQuery默认会降低它的含量。
接近 8 年之前 回复
dongma6326
dongma6326 有效。 大声笑!,感谢马丁和燃烧的codeigniter
接近 8 年之前 回复
dqc18251
dqc18251 以小写形式尝试json。 dataType:“json”
接近 8 年之前 回复
doujiang2020
doujiang2020 好的,那不是解决方案。
接近 8 年之前 回复



 数据类型:“JSON”,
</ code> </ pre>

应该是 是</ p>

  dataType:“json”,// json是小写的,T必须被捕获
</ code> </ pre>
</ div>

展开原文

原文

datatype: "JSON",

supposed to be

dataType: "json",  // json in lowercase and T has to be captalized



确保您拥有以下代码:</ p>

  if(isset($ _ POST)  ['getCustomersArray'])){
$ runQuery = mysql_query($ Query)或
die(“SQL:$ Query)&lt; br /&gt;”。mysql_error());

$ numrows = mysql_num_rows($ runQuery);

$ array = array(array());

for($ i = 0; $ i&lt; = 2; $ i ++){
$ row = mysql_fetch_array($ runQuery);
$ array [$ i] ['namn'] = $ row ['fornamn'];
}
header(“Content-Type:application / json; charset = UTF-8”);
print json_encode($ array);
}

</ code> </ pre>

在这里,您需要将content-type设置为application / json并设置正确的字符集以避免任何跨浏览器问题。 看看我的网站上的以下教程,它应该涵盖所有这些以及对您的代码的一些改进: PHP jQuery搜索教程 - 以正确的方式使用JSON对象 </ p>

希望这会有所帮助:)</ p>
</ div>

展开原文

原文

Make sure you have the following code:

if(isset($_POST['getCustomersArray'])){
$runQuery = mysql_query($Query) or
    die("SQL: $Query)<br />".mysql_error()); 
$numrows = mysql_num_rows($runQuery); 
$array = array(array());

for($i = 0;$i <= 2; $i++){
    $row = mysql_fetch_array($runQuery);
    $array[$i]['namn'] = $row['fornamn'];
}
header("Content-Type: application/json; charset=UTF-8");
print json_encode($array);
}

Here you need to set the content-type to application/json and set the correct charset to avoid any cross-browser issues. Take a look at the following tutorial from my website which should cover all of this and perhaps some improvements to your code: PHP jQuery Search Tutorial - using JSON object the proper way

Hope this helps :)

drzdu44226
drzdu44226 too long
接近 8 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问