【问题标题】:Getting JSON value from php using jquery ajax使用 jquery ajax 从 php 获取 JSON 值
【发布时间】:2013-12-13 04:38:35
【问题描述】:

嗨,朋友们可以请任何人给我。我是本章的新手..我正在尝试从 PHP 获取 JSON 格式值,但是在运行时我得到了这个输出“SyntaxError: JSON.parse: unexpected characte”.. 我想我在 php 转换中犯了错误......请帮帮我的朋友

我的 .html 文件

<html>
<head>
    <script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
    <title>Display Page</title>
</head>
<body>
    <button type='button' id='getdata'>GetData.</button>
    <button type='button' id='get'>sample</button>
    <script type='text/javascript'>
    $('#get').click(function() {
        alert("laksjdflk");
    });
    $(document).ready(function() {
        $('#getdata').click(function() {
            $.ajax({
                url:'neww.php' ,
                dataType:'json' ,
                success:function(output_string) {
                    alert(output_string);
                },
                error:function(xhr,ajaxOptions,thrownError){
                    alert(xhr.statusText);
                    alert(thrownError);
                }
            });
        });
    });
    </script>
</body>
</html>

生成php.php

<?php
    mysql_connect("localhost","root","");
    mysql_select_db("androidlogin");

    $sql=mysql_query("SELECT* FROM trysave");

    $temp="";
    $i=0;
    while($row=mysql_fetch_assoc($sql)){
        $temp=$row['stringss'];
        $temp.=$row['integerr'];
        $array[i]=$temp;
        i++;
    }
    echo json_encode($array);// this will print the output in json
?>

【问题讨论】:

标签: php jquery ajax json


【解决方案1】:

这可能是因为Undefined array variable notice 你必须define array 以防万一没有records found

您还错过了variable i 之前的$,它给出了错误(被视为常量,并且在您的代码中未定义),i 应该是$i 之类的,

$array=array();// define here to prevent from "Notice"
while($row=mysql_fetch_assoc($sql))
{
    $temp=$row['stringss'];
    $temp.=$row['integerr'];
    $array[$i]=$temp;// use $ before i
    $i++;// use $ before i
}
echo json_encode($array);// this will print the output in json

还有一件事你提到了PHP文件名generatephp.php,而你在$.ajax()中使用url:'neww.php' ,,你必须检查你的代码一次。

【讨论】:

    【解决方案2】:

    除了明显的问题(咳嗽 MySQL_*)之外,您的 PHP 文件应该在响应标头中指定输出将是 JSON 类型。输出默认为text/html,Javascript 无法将其解析为有效的 JSON 对象。

    你可以这样做

    <?php
    header('Content-type: application/json');
    // Rest of the code remains intact
    

    【讨论】:

      【解决方案3】:

      我会使用不同的东西......

      php:

      <?php
          mysql_connect("localhost","root","");
      
      
          $sql=mysql_query("SELECT stringss, integerr FROM androidlogin.trysave");
      
          $temp=array();
          $i=0;
          while($row=mysql_fetch_assoc($sql)){
              $temp[] = $row;
                 }
          echo json_encode($temp);// this will print the output in json
      ?>
      

      //你不需要 $i 变量,因为你会得到 java str.length = that $i

       <script type='text/javascript'>
          $('#get').click(function() {
              alert("laksjdflk");
          });
      
          $(document).ready(function() {
              $('#getdata').click(
              function() {
      jQuery.getJSON( "neww.php") //no get vars
      
      .done(function(a) {
      //console.clear();
      console.log(a);
      show_data(a);
      })
      
      .fail(function(a) {
      console.log( "error" );
      });
      
      });
      });
      
      
      function show_data(a){
      
      for(var i=0; i<a.length; i++)
      var stringss = a[i].stringss;
      var integerr = a[i].integerr;
      var nuber_temp = i;
      //do stuff with them ...
      
      }
          </script>
      

      如果问题仍然存在...尝试http://php.net/manual/ro/function.urlencode.php

      【讨论】:

        猜你喜欢
        • 2013-03-18
        • 1970-01-01
        • 1970-01-01
        • 2019-05-07
        • 2021-11-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多