简体   繁体   English

即使没有失败,AJAX也会失败吗?

[英]AJAX failing even though it's not failing?

I'm running an AJAX function like I've done before. 我正在像以前一样运行AJAX函数。 This time it's strange. 这次很奇怪。 While the XHR console gives me 200/Success, the error callback keeps happening. 虽然XHR控制台给我200 /成功,但错误回调不断发生。

function runNewprop() {
   var nurl = 'http://www.mysite.com/myscript.php';

$.ajax({
    url: nurl,
    dataType: 'json',
    success: function(data){
        $('#nl_details').html('');
        $each(data, function (key, value){
            var mlsnum = value[0];

            $('#nl_details').append('<div class="nl_list"><h5>'+mlsnum+'</h5></<div>');
        });
    },
    error: function(){
        alert('Oops!');
    }
});
}

Then the PHP file: 然后是PHP文件:

<?php
$link = mysql_connect('localhost','username','password');
mysql_select_db('singleprop', $link);
$date = mysql_real_escape_string($_GET['date']);
$sort = mysql_real_escape_string($_GET['sort']);
$query = "
    SELECT * FROM jos_mls
        JOIN jos_activeagents AS active ON singleprop.jos_mls.MSTLISTBRD = active.AGENTUID
        JOIN jos_agents AS agents ON active.AGENTUID = agents.AGTBRDIDMM
    ";

if ($date == 'week') {
    $query .= "AND MSTLISTDT >= DATE_ADD(CURDATE(), INTERVAL -7 DAY)";
}
elseif ($date == 'twoweek') {
    $query .= "AND MSTLISTDT >= DATE_ADD(CURDATE(), INTERVAL -14 DAY)";
}
elseif ($date == 'month') {
    $query .= "AND MSTLISTDT >= DATE_ADD(CURDATE(), INTERVAL -31 DAY)";
}
elseif ($date == 'twomonth') {
    $query .= "AND MSTLISTDT >= DATE_ADD(CURDATE(), INTERVAL -62 DAY)";
}



if ($sort == 'agent') {
    $query .= " ORDER BY AGTLNAME";
}
elseif ($sort == 'city') {
    $query .= " ORDER BY MSTCITY";
}
elseif ($sort == 'zip') {
    $query .= " ORDER BY MSTZIP";
}
elseif ($sort == 'county') {
    $query .= " ORDER BY MSTCOUNTY";
}
else {
    $query .= " ORDER BY MSTLISTDT";
}

$query .= ";";


$result = mysql_query($query);
$data = mysql_fetch_array($result);
return json_encode($data);

mysql_close($link);

?>

Am I missing something in my syntax? 我在语法中缺少什么吗? When I echo out the built query and copy it to my console and run it, everything goes smooth. 当我回显生成的查询并将其复制到我的控制台并运行它时,一切顺利。 But when I try to run this through AJAX, I get success/200, but the error callback happens. 但是,当我尝试通过AJAX运行它时,我获得了成功/ 200,但是发生了错误回调。

You have an error in the success callback. 您在成功回调中出错。 Change the following line: 更改以下行:

$each(data, function (key, value){

to

$.each(data, function (key, value){

The problem is your PHP is not outputting valid JSON. 问题是您的PHP无法输出有效的JSON。 You specified json as the datatype in the ajax call, this means jQuery will fire the error function if it could not parse the resulting JSON. 您在ajax调用中将json指定为数据类型,这意味着jQuery如果无法解析生成的JSON,则会触发error函数。

In your PHP you should be echoing not returning, change to: 在您的PHP中,您应该回显未返回的内容,请更改为:

echo json_encode($data);

Also verify nothing else is being outputted (such as error or notice messages). 还要确认没有其他任何输出(例如错误或通知消息)。

Finally, as @whirlwin points out, you have the $.each syntax error in the success function, while this isn't your root problem, it will become a problem when you fix the JSON output. 最后,正如@whirlwin指出的那样,成功函数中存在$.each语法错误,虽然这不是您的根本问题,但在修复JSON输出时将成为问题。

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

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