简体   繁体   English

成功地将json_encode的变量从PHP传递到外部.js文件?

[英]Successfully passing json_encode'd variables from PHP to external .js file?

First off let me say, I've never used JSON or AJAX before, but I think I understand the concept at least. 首先,我要说的是,我之前从未使用过JSON或AJAX,但我认为我至少了解该概念。

I'm only trying to pass some PHP values to a jquery function (located in an external .js file). 我只是想将一些PHP值传递给jquery函数(位于外部.js文件中)。 I've tried everything I can possibly think of, and don't know what's going wrong here. 我已经尝试了所有可能想到的方法,并且不知道这里出了什么问题。

In index.php I have: 在index.php中,我有:

<?php
$data = array(1 => 'thisor', 2 => 'thatttt');
echo json_encode($data);
?>

..which obviously isn't the real data I'm wanting to pass, but it's just for testing. ..这显然不是我想要传递的真实数据,但仅用于测试。 In my .js file I have the function: 在我的.js文件中,我具有以下功能:

  $.getJSON("index.php", function(data){
     alert('data loaded' + data);
  })
  .error(function() { alert("error"); });

and every time, it's throwing the error. 每次都抛出错误。 However I've noticed if I use $.post with the same exact code following it, it doesn't throw an error anymore, and returns the data. 但是我注意到如果我在$ .post之后使用相同的确切代码,它将不再抛出错误并返回数据。

Also, is it necessary to 'echo' the json_encode? 另外,是否有必要“回显” json_encode? What if I don't want this information displayed on my webpage? 如果我不希望此信息显示在我的网页上怎么办? Or.. I'm misunderstanding something here possibly. 或者..我可能在这里误会了一些东西。

Also x2, if it's worth noting, I read about 'header('Content-type: application/json');' 还有x2,如果值得注意的话,我读到有关“ header('Content-type:application / json');' that may need to go into the PHP file.. however when I did that, it no longer rendered the webpage in the browser, and instead just output the contents in plain text. 可能需要进入PHP文件。但是,当我这样做时,它不再在浏览器中呈现网页,而是仅以纯文本形式输出内容。 Am I needing to do this somewhere else / make another external PHP file? 我是否需要在其他地方执行此操作/创建另一个外部PHP文件?

Apologies for any ridiculous questions, it really is my first day trying to learn this thing. 对于任何荒谬的问题,我深表歉意,这真的是我尝试学习这件事的第一天。

It works fine for me. 这对我来说可以。 Are you sure that the ONLY thing output is the jon string? 您确定唯一的输出是jon字符串吗? Is there anything else at all in the index.php file? index.php文件中还有其他内容吗?

When using $.post, try changing your alert to: 使用$ .post时,尝试将警报更改为:

 alert('data loaded(' + JSON.stringify(data)+')');

and check that all you see is the json string. 并检查您所看到的只是json字符串。

Since you've said that the index.php file is also outputting other stuff, here is an example of what you COULD do (I'm not saying that this is good practice). 既然您已经说过index.php文件还会输出其他内容,那么这里是您可以做的一个例子(我并不是说这是一种好习惯)。 So you could write your index.php file similar to this: 因此,您可以编写类似于以下内容的index.php文件:

<?php
if(isset($_GET['ajax'])) {
  $data = array(1 => 'thisor', 2 => 'thatttt');
  echo json_encode($data);
  exit;
  }
?>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js">
</script>
<script type="text/javascript">
<!--
$.post("index.php?ajax", function(data){
     alert('data loaded(' + JSON.stringify(data)+')');
  })
  .error(function() { alert("error"); });
// -->
</script>

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

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