【问题标题】:Uncaught TypeError: Cannot read property 'data' of undefined未捕获的类型错误:无法读取未定义的属性“数据”
【发布时间】:2013-06-14 04:57:59
【问题描述】:

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

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 时,它显示:“Uncaught TypeError: Cannot read property "some value" of undefined"

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) 时它会停止工作并显示: “未捕获的类型错误:无法读取未定义的属性 'pl'”

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

谢谢,

现在我使用了一个外部服务器,它位于云中,如果我在 info.php 中使用常量 $id = "001",它就可以工作,但是当我删除该常量并将 $id = $_POST[' datos'] ,又是空的,所以我认为发送数据时有问题

【问题讨论】:

  • 您的 URL 结构看起来很奇怪。为什么是2个问号?可能是在两台不同服务器机器上运行的 php 代码版本之间存在一些差异,还是数据存在一些差异?
  • 我可能会在这里采摘稻草,但这似乎很不寻常,至少对我来说是');'; - 您是否尝试删除额外的;,例如')';
  • 成功回调的data变量中有什么?这可能是一个明确的错误,而不是您的数据,例如您的 IP 拒绝 MySQL 权限
  • 另外,使用控制台中的网络选项卡 (F12) 或使用类似 Fiddler 的软件检查 Ajax 调用的 HTTP 错误代码:fiddler2.com
  • 最后,尝试在新的浏览器选项卡中手动调用 URL。有一些浏览器插件允许发送 POST 变量以测试您的网页。

标签: php jquery ajax


【解决方案1】:

我认为有两点需要解决:

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,

【讨论】:

  • 当我使用 contentType : "application/json" 时会提示同样的问题并且没有数据
【解决方案2】:

最后我发现了问题的一部分,当它发送数据时,它是以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

 <?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 方式发送数据,但是通过这些更改,它现在正在运行,谢谢

【讨论】:

    猜你喜欢
    • 2021-12-22
    • 2015-01-06
    • 2017-07-26
    • 1970-01-01
    • 2019-02-26
    • 2021-12-25
    • 1970-01-01
    相关资源
    最近更新 更多