首页 文章

未捕获的TypeError:无法读取未定义的属性'data'

提问于
浏览
1

我需要一些关于此代码的帮助,我发现了一些类似的问题:

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

但仍然没有工作,我正在使用jquery(.ajax)向服务器(php)发送和接收数据,当我在.ajax函数的url中使用localhost时,它工作正常并接收我需要的数据,但是当我更改了它显示的服务器的ip的url:“未捕获的TypeError:无法读取未定义的属性"some value"”

Jquery代码:

$(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'
        });

}

这是PHP服务器文件(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) . ');';

?>

因此,当我使用url设置(localhost)时,它工作,当我更改为服务器IP(192.168.0.101)时,它停止工作并显示:“未捕获的TypeError:无法读取未定义的属性'pl'”

,我知道ip它正在工作,因为当我在我的浏览器上复制192.168.0.101/server/info.php它没有显示任何错误

谢谢,

现在我已经使用了一个外部服务器,它在 Cloud 端,如果我在info.php上使用常量$ id =“001”,它可以工作,但当我删除该常量并放入$ id = $ _POST ['datos']时,再次为空,所以我认为发送数据时有问题

2 回答

  • 0

    我认为有两件事需要解决:

    PHP:

    < header('Content-Type: text/javascript; charset=UTF-8');
    > header('Content-Type: application/json; charset=UTF-8');
    

    jQuery的:

    $.ajax({
        type: 'post',
        url: "http://192.168.0.101/server/info.php?jsoncallback=?",
        data: {datos:idscan},
        + contentType : "application/json"
        + dataType: "json"
        cache: false,
    
  • 0

    最后我发现了问题的一部分,当它发送数据时,它将它们作为Get发送,所以我改变了.ajax函数和info.php

    Jquery代码

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

    <?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) . ');';
    
        ?>
    

    我不确定为什么它将数据作为get发送,但是这些更改现在正在运行,谢谢

相关问题