简体   繁体   English

JSON.parse无法正常工作并退出功能

[英]JSON.parse does not work and exits function

Here's my problem 这是我的问题

I'm trying to replace this structure : 我正在尝试替换此结构:

  1. PHP: PHP:

     $mainContent = getContenu($_GET['rq']); die($mainContent); 
  2. JS: JS:

     $('#contenu').html(data); 

With this one : 有了这个:

  1. PHP: PHP:

     $envoi['mainContent'] = getContenu($_GET['rq']); die(json_encode($envoi)); 
  2. JS: JS:

     $('#contenu').html(JSON.parse(data).mainContent); 

With the first option, everything works just fine. 使用第一个选项,一切正常。 What happens is : In my JS file index.js , the snippet that I've written above is inside a structure as such : 发生的是:在我的JS文件index.js ,我上面编写的代码段位于这样的结构内:

$(document).ready(function(){
   (...)
   $('.menu a').click(function(){
      (...)
      $.get('index.php',rq=' + rq, function(data){
         //---->IT'S IN HERE<----//
      });
   });
});

The getContenu() function just returns the implode() version of an html or php file (depending on rq ). getContenu()函数仅返回html或php文件的implode()版本(取决于rq )。

The second option (using JSON) seems to exit the $.get function as soon as it hits JSON.parse() . 第二个选项(使用JSON)似乎在击中JSON.parse()立即退出$.get函数。 I've tried using the parse function outside of the $('#contenu').html() but var whatevs = JSON.parse(data) as well as var whatevs = JSON.parse(data).mainContent or (data)['mainContent'] all produce the same result. 我试过在$('#contenu').html()之外使用parse函数,但是var whatevs = JSON.parse(data)以及var whatevs = JSON.parse(data).mainContent(data)['mainContent']全部产生相同的结果。

If I put an alert before the JSON.parse function, it gets called, but if I put one after it, it doesn't call it, which has led me to believe that the JSON.parse is the problem. 如果在JSON.parse函数之前放置一个alert ,它将被调用,但是如果在它之后放置一个alert ,它将不被调用,这使我相信JSON.parse是问题所在。

I've tried to replace the getContenu function in the PHP part with some random string just to see if it would work, but nothing happens either. 我试图用一些随机字符串替换PHP部分中的getContenu函数,只是看它是否可以工作,但也没有任何反应。

Thanks for reading me, 谢谢你读我

Chris 克里斯

PS This is a school project so not using JSON is not an option as it is required by the teacher PPS I'm running it on the latest build of Chrome so browser's not the issue. PS这是一个学校项目,因此不使用JSON是不可选项,因为老师PPS要求我在最新版本的Chrome上运行它,因此浏览器不是问题。

OK so I don't know exactly what happened. 好,所以我不知道到底发生了什么。 For some reason it now works. 由于某种原因,它现在可以工作了。 I've not changed anything (I've browsed my Ctrl + Z and Ctrl + Maj + Z through and through to see what I changed but nothing is different, just some alerts and the console.log(data) that @ReijithRKrishnan suggested... But now it works without that line too. I'm speechless... IT is so full of mystery sometimes... 我没有做任何更改(我一直浏览Ctrl + Z和Ctrl + Maj + Z来查看更改的内容,但没有什么不同,只是@ReijithRKrishnan建议的一些警报和console.log(data) 。 ..但是现在它也不需要那条线了。我无语了...有时它充满了神秘色彩...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM