2012-11-19 07:04
浏览 69

jQuery ajax返回undefined

I am trying to return a row from my database where the id sent via jquery to the php matches a field value. I am getting back undefined and can't seem to work my way out of it.

My jquery:

function renderPreview( event ) {
    target = event.target.id;

    console.log(target) // should say the id name

            $('#results').addClass(target).html( $('#textInput').val() );
            $('html, body').animate({ scrollTop: 600}, "slow");

    console.log('start ajax')

        url: '../test.php',
        type: 'POST',
        data: [{'class' : target}],
        dataType: 'json',
        success: function(data) {
            var id = data[0];
            var name = data[1];
            var style = data[2];
            $('#codeTest').html("<b>id: </b><br />"+id+"<br /><b> name: </b><br />"+name+"<br /><b> style: </b><br />"+style);


$dbstylename = $_POST['class'];
$result = mysql_query("SELECT * FROM style where stylename like '$dbstylename'");
$array = mysql_fetch_row($result);

echo json_encode($array);


Also is there a line of code I can put in my jquery or php to see what query is going through in my chrome developer console?...like the console.logs I already have.

图片转代码服务由CSDN问答提供 功能建议

我试图从我的数据库中返回一行,其中通过jquery发送到php的id与字段值匹配。 我回来了未定义,似乎无法解决它。


  function renderPreview(event  ){
 target = event.target.id; 
 $('  #results')。addClass(target).html($('#textInput')。val()); 
 $('html,body')。animate({scrollTop:600},“slow”); \  n 
 console.log('start ajax')
 $ .ajax({
 url:'.. / test.php',
 data:[{'class  ':target}],
 var id = data [0]; 
 var name = data [1]; 
 var style = data [  2]; 
 $('#codeTest')。html(“&lt; b&gt; id:&lt; / b&gt;&lt; br /&gt;”+ id +“&lt; br /&gt;&lt; b&gt; name:&lt;  ; / b&gt;&lt; br /&gt;“+ name +”&lt; br /&gt;&lt; b&gt; style:&lt; / b&gt;&lt; br /&gt;“+ style); 


  $ dbstylename = $ _POST ['class']  ; 
 $ result = mysql_query(“SELECT * FROM style,其中stylename类似'$ dbstylename'”); 
 $ array = mysql_fetch_row($ result); 
echo json_encode($ array); 
mysql_close($  con); 

还有一行代码可以放在我的jquery或php中,以查看我的chrome中正在进行的查询 开发人员控制台?...就像我已经拥有的console.logs一样。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dshqd84261 2012-11-19 07:44

    The problem is that you are not sending the data in the correct way. jQuery is passing the value you assign to the data: property to jQuery.param. This function converts the data to a proper query string.

    But if you pass an array instead of an object, .param expects it to be an array of objects, where each of the objects has a name and value property.

    You can see what jQuery generates in your case by calling .param directly:

    > jQuery.param([{'class' : target}])

    You get the correct query string, if you pass either

    [{name: 'class', value: target}]


    {'class': target}

    Both generate:

    "class=<whatever the value of target is>"   
    打赏 评论

相关推荐 更多相似问题