简体   繁体   中英

AJAX failing even though it's not failing?

I'm running an AJAX function like I've done before. This time it's strange. While the XHR console gives me 200/Success, the error callback keeps happening.

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
$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.

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. 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.

In your PHP you should be echoing not returning, change to:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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