简体   繁体   English

Jquery ajax json解析错误

[英]Jquery ajax json Parse error

when i pass ajax values it says json parseererror, tried jsonp but not working. 当我传递ajax值时,它说json parseererror,尝试了jsonp但没有工作。 Here's my code: 这是我的代码:

HTML: HTML:

<form class="ajaxform" method="post">
    <div class="form-group form-inline">
        <label for="searchtxt">Search Here: </label>
        <input type="text" name="ajaxinput" class="form-control ajaxinput">
    </div>
    <div class="form-group">
        <a href="" class="ajaxsubmit btn btn-primary">Get Data</a>
    </div>
</form>

PHP: PHP:

$return_array = array();

$query = "select * from tbl_admin where uName like '%".$ajaxinput."%' or uEmail like '%".$ajaxinput."%'";

$abc = $db->pdoQuery($query)->results();

foreach ($abc as $key => $value) {
    # code...
    $return_array['admin_name'] = $value['uName'];
    $return_array['admin_email'] = $value['uEmail'];
    $return_array['admin_ip'] = $value['ipAddress'];

    echo json_encode($return_array);
}
exit();

JQUERY: JQUERY:

$('.ajaxsubmit').on('click', function(e){  
e.preventDefault();
var urlPath = siteName+'modules-nct/ajax-nct/ajax.ajax-nct.php';
var mydata = jQuery(".ajaxform").serialize();

$.ajax({
url: urlPath,
data : mydata,
dataType: 'json',

success: function(response){
  console.log(response);
  alert(response);

},
error: function(xhr, status){
  console.log(status);
}
});
});

i want data in json as i have to append it in a table. 我想要json中的数据,因为我必须将它附加到表中。 In datatype: html data comes but not that useful as it contains multiple records. 在数据类型:html数据来但不是很有用,因为它包含多个记录。

You are echo ing the result every loop . 你在每个loopecho结果。 That is the reason why your js cant parse it correctly. 这就是为什么你的js无法正确解析它的原因。 You should echo it once(on the end of the loop.). 您应该echo一次(在循环结束时)。

$return_array = array();

foreach ($abc as $key => $value) {
    $temp = array();
    $temp['admin_name'] = $value['uName'];
    $temp['admin_email'] = $value['uEmail'];
    $temp['admin_ip'] = $value['ipAddress'];

    $return_array[] = $temp;
}

 echo json_encode($return_array);

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

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