dscrb00804 2013-06-14 04:57
浏览 657
已采纳

未捕获的TypeError:无法读取未定义的属性“数据”

I need some help with this code, I have found some similar issues:

Uncaught TypeError: Cannot read property 'value' of undefined

Uncaught TypeError: Cannot read property 'name' of undefined

Ajax call in jQuery - uncaught typeerror cannot read property 'error' of null

but still not working, I'm using jquery (.ajax) to send and recieve data to the server (php), when I use localhost at url in .ajax function, it works fine and receives the data that I need, but when I change the url for the ip of the server it shows: "Uncaught TypeError: Cannot read property "some value" of undefined"

Jquery code:

$(document).on("ready",inicio);

function inicio(){



  /*Change pages events*/
  var buttonscan;
  buttonscan = $('#button_scan');
  buttonscan.on("click",solicitardatos);



}

function solicitardatos(){
           var idscan = "001";
            $.ajax({
                type: 'post',
                //with localhost works fine, this is the ip of my cpu when testing from
                //other device 192.168.0.101
                url: "http://192.168.0.101/server/info.php?jsoncallback=?",
                data: {datos:idscan},
                cache: false,
                success: function(data) {


                $('#button_scan').hide();
                $('#page1').hide();
                $('#page2').show(); 


                $('#pl').html(data[0].pl);                 
                $('#name').html(data[0].name);


            },
            error: function(jqXHR, textStatus, errorThrown) {
                console.log("Error... " + textStatus + "        " + errorThrown);
                alert("Error... " + textStatus + "        " + errorThrown);
            },
            dataType: 'json'
        });

}

And this is the PHP server file (info.php)

<?php 
header('Content-Type: text/javascript; charset=UTF-8');

error_reporting(E_ALL ^ E_NOTICE);
require("conexion.php");


$id = $_POST['datos']; 

/*Here is all the sql statements, and the mysqli query*/

while ($fila = mysqli_fetch_array($peticion)) {

    $resultado[] = array("pl"=>$fila['pl'],"name"=>$fila['name']);  
}



mysqli_close($conexion);
//echo json_encode($resultado);
echo $_GET['jsoncallback'] . '(' . json_encode($resultado) . ');';

?>

So when I use the url setting (localhost) it works, when I change to the server ip (192.168.0.101) it stops working and shows: "Uncaught TypeError: Cannot read property 'pl' of undefined"

, I know that the ip it is working because when I copy 192.168.0.101/server/info.php at my browser it doesn't shows any error

Thanks,

Now I have used an external server, which is in cloud, and it works if I use a constant $id = "001" at info.php, but when I removed that constant and put $id = $_POST['datos'] ,is empty again, so I think there is something wrong when sending the data

  • 写回答

2条回答 默认 最新

  • dongxun6690 2013-06-15 14:24
    关注

    Finally I found some part of the issue, when it sends the data, it was sending them as Get, so I changed both .ajax function and info.php

    Jquery code

    function solicitardatos(){
      $.ajax({
                    type: 'get',                
                    url: "http://192.168.0.101/server/info.php?jsoncallback=?",
                    data: {datos:idscan},
                    dataType: "json",
                    cache: false,
                       ...
    
                 });
    }
    

    Php

     <?php 
        header('Content-Type: text/javascript; charset=UTF-8');
    
        error_reporting(E_ALL ^ E_NOTICE);
        require("conexion.php");
    
    
    if (isset ($_GET['datos'])) { 
    $id = $_GET['datos']; 
    
        /*Here is all the sql statements, and the mysqli query*/
    
        while ($fila = mysqli_fetch_array($peticion)) {
    
            $resultado[] = array("pl"=>$fila['pl'],"name"=>$fila['name']);  
        }
      } else {}  
    
    
        mysqli_close($conexion);
    
        echo $_GET['jsoncallback'] . '(' . json_encode($resultado) . ');';
    
        ?>
    

    I'm not sure why it was sending data as get, but with these changes it is now running , Thanks

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决