dongxue7306 2014-01-26 21:28
浏览 161
已采纳

ajax请求不返回值

I have an ajax request which gets a json string but for some reason it has started returning "undefined", The json string does return valid JSON but its just not working!

Ajax request:

 if (editSeq) {
        $.ajax({
            url: './json/admin/getData.php',
            type: 'POST',
            async: false,
            data: { SEQ: editSeq },
            dataType: 'json',
            success: function (data) {
                var HTML = data.HTML;
                $('#blankform').append(HTML);
                alert(HTML);
            }
        });

JSON:

<?php
include("../../includes/db.php");
$SEQ = $_POST["SEQ"];
$sth = sqlsrv_query($conn,"SELECT HTML from TBL_DATA WHERE Sequence = " . $SEQ);
$rows = array();
while($r = sqlsrv_fetch_array($sth,SQLSRV_FETCH_ASSOC)) {
    $rows[] = $r;
}
if( $sth === false)
{
     echo "Error in query preparation/execution.
";
     die( print_r( sqlsrv_errors(), true));
}
print json_encode($rows);
?>

This is my JSON response:

   [
     {
    "HTML": "  

<div id=\"div_143\" style=\"width: 300px; white-space: nowrap; padding-right: 50px; left: 60px; top: 48px;\" class=\"ui-resizable ui-draggable ui-resizable-disabled ui-state-disabled\" aria-disabled=\"true\"><label> Incident #&nbsp;<input type=\"text\" style=\"width:100%;\" id=\"input_143\" role=\"textbox\" aria-autocomplete=\"both\" aria-disabled=\"false\" aria-readonly=\"false\" aria-multiline=\"false\" class=\"jqx-widget-content jqx-widget-content-web jqx-input jqx-input-web jqx-widget jqx-widget-web jqx-rc-all jqx-rc-all-web\" placeholder=\"\" disabled=\"disabled\"><\\/label><div class=\"ui-resizable-handle ui-resizable-e\" style=\"z-index: 90;\"><\\/div><div class=\"ui-resizable-handle ui-resizable-w\" style=\"z-index: 90;\"><\\/div><div class=\"ui-resizable-handle ui-resizable-sw\" style=\"z-index: 90;\"><\\/div><div class=\"ui-resizable-handle ui-resizable-ne\" style=\"z-index: 90;\"><\\/div><div class=\"ui-resizable-handle ui-resizable-nw\" style=\"z-index: 90;\"><\\/div><div class=\"ui-resizable-handle ui-resizable-se ui-icon ui-icon-gripsmall-diagonal-se\" style=\"z-index: 90;\"><\\/div><\\/div>"
     }
   ]
  • 写回答

2条回答 默认 最新

  • douyin7829 2014-01-26 21:40
    关注

    First of all, in AJAX scripts always terminate the script after you echo the output. Say, if you have something more down the script, like other IF's, no need to run them if you have already displayed JSON output, or the extra output can mess your JSON and everything will stop working.

    So, always do:

    print json_encode($rows);
    exit;
    

    Second of all, in your case you might have some SQL problems and all your problem is you don't see the error message. It may say "Error in query preparation/execution" but you just are not able to see it! Or you can have any other PHP fatal errors which you just can't see, as it messes up the JSON and Javascript can't display the .HTML part.

    Your solution is to test it, temporarily changing the Javascript to:

    $.ajax({
         url: './json/admin/getData.php',
         type: 'POST',
         async: false,
         data: { SEQ: editSeq },
         // dataType: 'json', // temporarily comment it out to receive plain messages
         success: function (data) 
         {
           alert(data); return; // temporary line. Remove after debuging.
           var HTML = data.HTML;
           $('#blankform').append(HTML);
           alert(HTML);
         }
    });
    

    And third of all, (after you added your JSON output) you seem to have an Array the topmost element of JSON, if it starts with [{"HTML":

    To work with array you should change the JavaScript to:

    var HTML = data[0].HTML;
    

    or even, if there may be many rows:

    var HTML = '';
    for(i=0;i<data.length;i++) HTML += data[i].HTML;\
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突