2014-03-27 143 views
0

当我用JSON执行我的页面时,总是有这样的响应:[object object]。我在其他岗位已经阅读是不存在的领域的问题,但这个错误出现太大评论响应JSON中的[对象对象]

$.each(result, function(i, row) { 
    console.log(JSON.stringify(row)); 
    $('#data-list').append('<li><a href="" data-id="' + row.id+ '"><h3>' + row.nombre + '</h3><p>' + row.denominacion + '</p></a></li>'); 
}); 

我的JavaScript:

$(document).on('pageinit', '#home', function(){  
      var stuff = { 
       id:null, 
       nombre:null, 
       precio:null, 
       denominacion:null 
       }; 
      var jsonString = JSON.stringify(stuff); 

    $.ajax({ 
      url: "http://www.domain.com/ws.php?TIPO=OK" , 
      crossDomain: true, 
      type:"GET", 
      contentType: "application/json; charset=utf-8", 
      dataType: "jsonp", 
       async: true, 
       data: jsonString, 
       success: function (result) { 
        alert(result); 
        $.each(result, function(i, row) { 
        console.log(JSON.stringify(row)); 
        $('#data-list').append('<li><a href="" data-id="' + row.id+ '"><h3>' + row.nombre + '</h3><p>' + row.denominacion + '</p></a></li>'); 
        }); 
       $('#data-list').listview('refresh'); 
       }, 
       error: function(xhr, status, error){ 
          console.log(status + '; ' + error+ ';');}, 
       jsonpCallback:function(response) { 
          console.log('callback success'+response); 
       } 
      });   
     }); 

在Web服务器是非常简单的只是做一个选择和encapsule JSON,我尝试与内容类型:应用程序/ JavaScript”和‘内容类型:应用程序/ JSON’,但同样的结果,这就是Web服务代码

<?php 
    include($DIRCONF . 'conf/VARIABLES.ini.php'); //incluimos configuración 
    include($DIRCONF . 'JSON.php'); $tipo=$_GET['TIPO']; $json = new Services_JSON; 

$conexion = mysql_connect(SERVIDOR_MYSQL, USUARIO_MYSQL, PASSWORD_MYSQL); mysql_select_db(BASE_DATOS, $conexion); 

$que = "SELECT * FROM `DATOS`"; 

$res = mysql_query($que, $conexion) or die(mysql_error()); 

while ($row = mysql_fetch_assoc($res)) { 
    $data[] = $row; } //Cerramos la conexion a la base de datos mysql_close($conexion); 


//header("Content-type: application/javascript"); header("Content-type: application/json"); echo json_encode($data) ; ?> 

回答

0

我能找到解决的问题是web服务,如果您使用JSONP:“jsoncallback”需要WS返回$ _GET [“jsoncallback”]如何JSON对象,如果不把它,不要找不到任何对象并做错误。

例如:

echo $_GET['jsoncallback'] . '(' . json_encode($records) . ');'; 
1

[object object]是什么JavaSc当你试图使用一个对象,就好像它是一个字符串一样,ript会给你。你的代码可能是好的,但console.log('callback success'+response);永远不会记录有用的信息。这威力工作:

console.log('callback success: ' + JSON.stringify(response)); 
+1

console.dir也可能是一种选择 – alou

+0

此日志显示 “回调的成功:未定义” page.html.html(拉利内阿44)(日志线) parsererror ;错误:undefined未被调用; [object Object],这是什么意思,男人到达函数回调未定义值? page.html(línea41)(这行是功能错误的地方) – Roberto