简体   繁体   English

从JQuery中的PHP数组中读取

[英]Reading from a PHP array in JQuery

I seem to have trouble with getting data from a php array, I've looked at various examples of how to do this but I must be missing something because I can not get my code to work. 我似乎在从php数组中获取数据时遇到了麻烦,我已经查看了各种如何执行此操作的示例,但我必须遗漏一些东西,因为我无法让代码工作。

PHP method PHP方法

function getLatestActivity(){
   $messages = array( 
      array('id'=>'01', 'value'=>'blah'),
      array('id'=>'02', 'value'=>'Oil'),
      array('id'=>'03', 'value'=>'Spark') 
   );

   echo json_encode($messages); 
   return;  
}

AJAX get functiom AJAX获得了功能

function getLatestActivities(){
   $.ajax({
      type: "GET", url: "include/process.php", 
      data:{
         getLatestActivity: "true",
         toUser: "4",
         ignoreMessages:"1",
         dataType: "json"
      },
      success: function(data){
         $.each(data, function (i, elem) {
            alert(data.id);
         });              
      }
   });   
}

The alert prints out the message "undefined", any help would be appreciated thank you. 警报打印出“未定义”消息,任何帮助将不胜感激,谢谢。

尝试 - 它现在是你的单个对象:

alert(elem.id);

It is quite likely that data isn't what you're expecting. 数据很可能不是您所期望的。 You aren't setting any headers in your response, and as hinted at by @Sudesh dataType isn't in the right place. 您没有在响应中设置任何标题,正如@Sudesh所示,dataType不在正确的位置。 The result is that data is most likely a string because jquery will not parse it as json. 结果是数据很可能是一个字符串,因为jquery不会将其解析为json。

You're also referring to the wrong variable. 你也指的是错误的变量。 data.id cannot exist with the data you are returning from php. data.id不能与您从php返回的数据一起存在。 you would need elem.id or if you prefer data[i].id if data did contain what you're expecting. 你需要elem.id或者你更喜欢data[i].id如果数据确实包含了你期望的内容。 As an aside - using $.each instead of a for loop is relatively inefficient. 暂时 - 使用$ .each而不是for循环是相对低效的。

You can check if that's the case with code such as: 您可以检查代码是否是这种情况,例如:

function getLatestActivities(){
   $.ajax({
      type: "GET", url: "include/process.php", 
      data:{
         getLatestActivity: "true",
         toUser: "4",
         ignoreMessages:"1",
         dataType: "json"
      },
      success: function(data){
         console.log(data); // <- look at the response
         $.each(data, function (i, elem) {
            // alert(data.id); don't debug using alerts
            console.log(elem);
         });              
      }
   });   
}

If data is a string (or simply, not what you're expecting) this should get you closer: 如果数据是一个字符串(或简单地说,不是你期望的那样),那么这应该让你更接近:

function getLatestActivity(){
    ....
    header('Content-type: application/json');
    echo json_encode($messages); 
}

with

function getLatestActivities(){
   $.ajax({
      url: "include/process.php", 
      data:{
         getLatestActivity: true,
         toUser: 4,
         ignoreMessages:1,
      },
      success: function(data){
         console.log(data);           
         ...              
      }
   });   
}

data should at least then be an array. data至少应该是一个数组。

I think dataType should not be part of data object rather should be sibling of success (options). 我认为dataType不应该是数据对象的一部分,而应该是成功的兄弟(选项)。 I believe you are not getting data as JSON object rather just string because of it 我相信你不会因为它而将数据作为JSON对象而不仅仅是字符串

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

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