JSON.parse()不起作用 [英] JSON.parse() isn't working

查看:162
本文介绍了JSON.parse()不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么我的 json.parse()不起作用。错误可能是在我眼前,但我找不到它。以下是我的代码:

 <!DOCTYPE html> 
< html>

< head>
< meta charset =UTF-8>
< title> Stage galerij< / title>
< link rel =stylesheethref =css / main.css>
< script src =https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js>< / script>
< / head>

< body>
< div id =id01>< / div>
< script>
var xmlhttp = new XMLHttpRequest();
var url =http://www.clouddesk.be/galerij/includes/galerij_array.php;
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4&& xmlhttp.status == 200){
myFunction(xmlhttp.responseText);
}
};
xmlhttp.open(GET,url,true);
xmlhttp.send();

函数myFunction(响应){
var arr = JSON.parse(response);
var i;
var out =< table>; (i = 0; i out + =< tr>< td>的

+
arr [i] .id +
< / td>< td> +
arr [i] .File_path +
< / td>< td> +
arr [i] .uitleg +
< / td>< / tr>;
}
out + =< / table>;
document.getElementById(id01)。innerHTML = out;
}
< / script>
< / body>

< / html>

这里是php文件的代码:

 <?php 
header(Access-Control-Allow-Origin:*);
header(Content-Type:application / json; charset = UTF-8);

$ conn = new mysqli(localhost,clouddesk_robbe,password,clouddesk_stage);

$ result = $ conn->查询(SELECT * FROM Fotos);

$ outp =[;
while($ rs = $ result-> fetch_array(MYSQLI_ASSOC)){
if($ outp!=[){$ outp。=,;}
$ outp 。='{id:'。$ rs [idFotos]。',';
$ outp。='File_path:'。$ rs [Foto_File]。',';
$ outp。='uitleg:'。$ rs [Uitleg]。',';
$ outp。='alt:'。$ rs [Alternatief]。'}';
}
$ outp。=];

$ conn-> close();

echo($ outp);
?>

它输出这个json字符串:

  [{id:2,File_path:img\gallerij\Afbeelding-61.png,uitleg:Zebra verliest zijn strepen。, alt:Stressige zebra},{id:3,File_path:img\gallerij\download(1).jpg,uitleg:Tomatensoep meet balletjes:Nog een keer blazen ,alt:Grappige cartoon},{id:4,File_path:img\gallerij\download.jpg,uitleg:Brilsmurf会见een raar gezicht,alt:Brilsmurf},{id:5,File_path:img\gallerij\download(2).jpg,uitleg:Mooi avondlijk woestijnuitzicht。,alt:Woestijnuitzicht},{id:8,File_path:img\gallerij\image1.jpg,uitleg:Een mooi blaadje dat door het herfstseizoen van de boom is gevallen en eenzaam op de grond licht。Het is getrokken in de late herfstzon。,alt:Herfstblaadje op de grond},{id:9,File_path:img\\ \\\\\\\\\\\\, leurd。,alt:Een kleurig paneel},{id:10,File_path:img\gallerij\images(1).jpg,uitleg:Mooie foto van een高尔夫死于avond getrokken。 Jeziet de reflectie van de zo in de buiging van de golf。,alt:de avondzon中的高尔夫球},{id:12,File_path:img\gallerij\images。 jpg,uitleg:Mooie auto,het is een dure rode lexus getrokken langs de linker voorkant。,alt:Mooie rode lexus},{id:13,File_path img\gallerij\Nikon-D810-Image-Sample-6.jpg,uitleg:Een uitzicht op een onbewoond eiland zoals je je voorstelt in je verbeelding,alt:Een onbewoond eiland}, {id:14,File_path:img\gallerij\image-slider-2.jpg,uitleg:Het huis van de film in de lucht doormiddel van duizenden ballonen aan de schoorsteen 。,alt:Huis in de lucht},{id:15,File_path:img\gallerij\demo-image.jpg,uitleg:Een mooie foto van een zwaan。,alt:foto van een zwaan}] 

一切从w3schools.com的这个页面:
http://www.w3schools.com/ JSON / json_example .asp
当我使用w3schools.com的原始代码对它进行测试时,它完美运行。但是,当我更改URL变量和数组名称时,它不再起作用。我还测试了json输出是否与在线验证器一起生效,并且它表示完全正常。

JSON.parse )不工作,因为json字符串不正确。 File_path
img\gallerij\Afbeelding-61.png 但它应该是 IMG / gallerij /演示image.jpg的。请在json字符串中随处更改 File_path 点击 here for jsfiddle example。

File_path is从生成$ outp。='File_path:'。$ rs [Foto_File]。','; \\ 与 /



好处:您使用<$ c $在客户端直接使用 img / gallerij / demo-image.jpg ,这样这个路径可以直接用作 src DOM元素。


I don't understand why my json.parse() isn't working. The error is probably right before my eyes but I can't find it. Here is the code that I have:

<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <title>Stage galerij</title>
  <link rel="icon" href="img/website/photo.jpg" type="image/jpg" sizes="any">
  <link rel="stylesheet" href="css/main.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
</head>

<body>
  <div id="id01"></div>
  <script>
    var xmlhttp = new XMLHttpRequest();
    var url = "http://www.clouddesk.be/galerij/includes/galerij_array.php";
    xmlhttp.onreadystatechange = function() {
      if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        myFunction(xmlhttp.responseText);
      }
    };
    xmlhttp.open("GET", url, true);
    xmlhttp.send();

    function myFunction(response) {
      var arr = JSON.parse(response);
      var i;
      var out = "<table>";

      for (i = 0; i < arr.length; i++) {
        out += "<tr><td>" +
          arr[i].id +
          "</td><td>" +
          arr[i].File_path +
          "</td><td>" +
          arr[i].uitleg +
          "</td></tr>";
      }
      out += "</table>";
      document.getElementById("id01").innerHTML = out;
    }
  </script>
</body>

</html>

And here is the code of the php file:

<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");

$conn = new mysqli("localhost", "clouddesk_robbe", "password", "clouddesk_stage");

$result = $conn->query("SELECT * FROM Fotos");

$outp = "[";
while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
    if ($outp != "[") {$outp .= ",";}
    $outp .= '{"id":"'  . $rs["idFotos"] . '",';
    $outp .= '"File_path":"'   . $rs["Foto_File"]        . '",';
    $outp .= '"uitleg":"'  . $rs["Uitleg"] . '",';
    $outp .= '"alt":"'. $rs["Alternatief"]     . '"}'; 
}
$outp .="]";

$conn->close();

echo($outp);
?>

It outputs this json string:

[{"id":"2","File_path":"img\gallerij\Afbeelding-61.png","uitleg":"Zebra verliest zijn strepen.","alt":"Stressige zebra"},{"id":"3","File_path":"img\gallerij\download(1).jpg","uitleg":"Tomatensoep met balletjes: Nog een keer blazen en ik sla op je bek.","alt":"Grappige cartoon"},{"id":"4","File_path":"img\gallerij\download.jpg","uitleg":"Brilsmurf met een raar gezicht","alt":"Brilsmurf"},{"id":"5","File_path":"img\gallerij\download(2).jpg","uitleg":"Mooi avondlijk woestijnuitzicht.","alt":"Woestijnuitzicht"},{"id":"8","File_path":"img\gallerij\image1.jpg","uitleg":"Een mooi blaadje dat door het herfstseizoen van de boom is gevallen en eenzaam op de grond licht. Het is getrokken in de late herfstzon.","alt":"Herfstblaadje op de grond"},{"id":"9","File_path":"img\gallerij\7.jpg","uitleg":"Een kleurrijk paneel dat elke mens wel terug wat opfleurd.","alt":"Een kleurig paneel"},{"id":"10","File_path":"img\gallerij\images(1).jpg","uitleg":"Mooie foto van een golf die in de late avond getrokken is. Je ziet de reflectie van de zo in de buiging van de golf.","alt":"Golf in de avondzon"},{"id":"12","File_path":"img\gallerij\images.jpg","uitleg":"Mooie auto, het is een dure rode lexus getrokken langs de linker voorkant.","alt":"Mooie rode lexus"},{"id":"13","File_path":"img\gallerij\Nikon-D810-Image-Sample-6.jpg","uitleg":"Een uitzicht op een onbewoond eiland zoals je je voorstelt in je verbeelding","alt":"Een onbewoond eiland"},{"id":"14","File_path":"img\gallerij\image-slider-2.jpg","uitleg":"Het huis van de film up in de lucht doormiddel van duizenden ballonen aan de schoorsteen.","alt":"Huis in de lucht"},{"id":"15","File_path":"img\gallerij\demo-image.jpg","uitleg":"Een mooie foto van een zwaan.","alt":"foto van een zwaan"}]

I copied practically everything from this page of w3schools.com: http://www.w3schools.com/json/json_example.asp When I tested it with the original code of w3schools.com it worked perfectly. But when I changed the URL variable and the array names, it doesn't work anymore. I also tested if the json ouput was valid with an online validator and it said it was completely fine.

解决方案

JSON.parse() is not working because json string is not proper. File_path is img\gallerij\Afbeelding-61.png but it should be img/gallerij/demo-image.jpg. Please change File_path everywhere in json string .

Click here for jsfiddle example.

File_path is getting generated from $outp .= '"File_path":"'. $rs["Foto_File"] . '",'; So replace \ with /.

Benefit: You get a path using JSON.parse() like img/gallerij/demo-image.jpg directly in client, so that this path can be used directly as src of DOM element.

这篇关于JSON.parse()不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆