2014-10-10 222 views
-3

我会做一些ajax调用来保存数据库中的数据。 当我使用responseText时,我收到未定义的。Ajax返回undefined

这是.js文件:

function init(){ 
alert('loaded'); 
$('#fd-table-1').find('tr').click(function() { 
       var idVar = $(this).find('td').eq(0).text(); 
       getData(idVar); 
}); 

function getData(idVar){ 
    xmlhttp = new XMLHttpRequest(); 
    xmlhttp.open('POST','api.php',true); 
    xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); 
xmlhttp.onreadystatechange=function() 
    { 
    if (xmlhttp.readyState==4 && xmlhttp.status==200) 
    { 
    alert(xmlhttp.respondeText); 
    } 
    } 

    xmlhttp.send('id='+idVar); 

} 

} 

window.addEventListener('load',init,false); 

,这是api.php:

<?php 

    //-------------------------------------------------------------------------- 
    // 1) Connect to mysql database 
    //-------------------------------------------------------------------------- 
    include 'DB.php'; 
    $con = mysql_connect($host,$user,$pass) or die('1'); 
    $dbs = mysql_select_db($databaseName, $con)or die('2'); 

    //-------------------------------------------------------------------------- 
    // 2) Query database for data 
    //-------------------------------------------------------------------------- 
    $result = mysql_query("SELECT * FROM ".$tableName." WHERE user_id=".$_POST['id'])or die('3');   //query 
    $array = mysql_fetch_row($result)or die('4');       //fetch result  
    //-------------------------------------------------------------------------- 
    // 3) echo result as json 
    //-------------------------------------------------------------------------- 
    echo json_encode($array); 

?> 

该网站是在Laravel4 发展为什么我收到 “未定义”?

谢谢

+6

你为什么不使用jQuery的['$ .ajax'(HTTP:/ /api.jquery.com/jQuery.ajax/)?对于这个问题:'respondeText'需要是'responseText' – kero 2014-10-10 12:03:24

+0

可能是由于'ASYNC'操作导致的 – senK 2014-10-10 12:04:50

+4

'alert(xmlhttp.respondeText);''alert'(xmlhttp.responseText);' – Gowri 2014-10-10 12:05:48

回答

1

更换

alert(xmlhttp.respondeText); 

alert(xmlhttp.responseText); 
+0

你刚刚取消删除答案或它是如何弹出,它说“5分钟前”? – kero 2014-10-10 12:09:44

+0

您直接从评论中提取...... – 2014-10-10 12:11:54

0
$.ajax({ 
    'global': false, 
    'url': "api.php", 
    'dataType': "json", 
    'success': function (data) { 
     console.log(data); 
    } 
}); 

检查了这一点。愿这对你有所帮助......

+0

除非需要,否则不应添加'global:false',并且'dataType:json'也是不必要的 - jQuery将从返回的Content-type中找出结果。 – Alnitak 2014-10-10 12:15:38

+0

是的你是对的..... – 2014-10-10 13:36:23

1

您有jQuery的 - 你的整个AJAX功能可以用这个来代替:

function getData(idVar) { 
    return $.post('api.php', {id: idVar}).then(function(data) { 
     alert(data); 
    }); 
} 

使您respondeText错字完全没有实际意义!

0

其更好的和易于使用jQuery由@kingkero

function ajaxCall()   // normal call 
//function ajaxCall(page,display) //can add some parameters if needed 
    {   
     //required only if you want to send some data 
     var uid   = <?echo $userId;?> //can also use php values 
     var uid   = "something"   //normal declration   

     var formdata  = new FormData();  

     formdata.append('uid',uid); 
     formdata.append('name',"ronser"); //can include many data through append 


     $.ajax({ 

       url   : 'yourLink.php', 
       dataType : 'text', 
       cache  : false, 
       contentType : false, 
       processData : false, 
       data  : bookDet, 
       type  : 'post', 
       success  : function(data){ 
          // alert(data); 
          document.getElementById("yourID").innerHTML=data; 

      } 
     }); 

    } 

提及应罚款的$.ajax ...