NaN error in ajax callback
NickName:bollo Ask DateTime:2011-09-18T01:40:04

NaN error in ajax callback

I am getting a NaN error in my ajax callback function and can only think it has to do with an array in PHP. I have been trying to find ways to correct it but have come up against a brick wall.

What is supposed to happen is that PHP queries the database and if there are no results send a response to ajax and issue the error message. However, all I am getting is NaN. The error stems from the success code below.

I would be grateful if someone could point out my error.

PHP code:

$duplicates = array();

foreach ($boxnumber as $val) {
    if ($val != "") {
        mysql_select_db($database_logistor, $logistor);
        $sql = "SELECT custref FROM boxes WHERE custref='$val' and status = 'In'";
        $qry = mysql_query($sql) or die(mysql_error());

        if (mysql_num_rows($qry) < 1) {
            $duplicates[] = '[ ' . $val . ' ]';
            $flag = 1;
        } else {
            $duplicates[] = $val;
        }
    }
}

//response array with status code and message
$response_array = array();
if (!empty($duplicates)) {
    if ($flag == 1) {
//set the response
        $response_array['status'] = 'error';
        $response_array['message'] = 'ERROR: ' . implode(',', $duplicates) . ' needs to be in the database to be retrived.';
    }
//if no errors
} else {

//set the response
    $response_array['status'] = 'success';
    $response_array['message'] = 'All items retrieved successfully';
    $response_array['info'] = ' You retrieved a total of: ' . $boxcount . ' boxes';
}

//send the response back
echo json_encode($response_array);

Relevant ajax:

$("#brtv-result").html(msg.message+msg.info);

jQuery code:

$(function() {

 $("#BRV_brtrv").submit(function() {

   var send = $(this).serialize();

    $.ajax({
      type: "POST",
      url: "boxrtrv.php",
      cache: false,
      data: send,
      dataType: "json",
      success: function(msg) {
       if( msg.status === 'error') {
          $("#brtv-result").fadeIn(1000).delay(1000).fadeOut(1000);
          $("#brtv-result").removeClass('error');
          $("#brtv-result").removeClass('success');
          $("#brtv-result").addClass(msg.status);
          $("#brtv-result").html(msg.message);
       }

       else {
          $("#brtv-result").fadeIn(2000).delay(2000).fadeOut(2000);
          $("#brtv-result").removeClass('error');
          $("#brtv-result").addClass('success');
          $("#brtv-result").addClass(msg.status);
          $("#brtv-result").html(msg.message+msg.info);
          //location.reload(true);
          //$('#brtv-result').addClass("result_msg").html("You have successfully retrieved: "+data.boxnumber).show(1000).delay(4000).fadeOut(4000);
          $("#BRV-brtrv-slider").val(0).slider("refresh");
          $("input[type='radio']").attr("checked",false).checkboxradio("refresh");
          var myselect = $("select#BRV-brtrv-department");
          myselect[0].selectedIndex = 0;
          myselect.selectmenu("refresh");
          var myselect = $("select#BRV-brtrv-address");
          myselect[0].selectedIndex = 0;
          myselect.selectmenu("refresh");
      }

     },
      error:function(){
         $("#brtv-result").show();
         $("#brtv-result").removeClass('success');
         $("#brtv-result").addClass('error');
         $("#brtv-result").html("There was an error submitting the form. Please try again.");
     }
   });
   return false;
  });
});

Copyright Notice:Content Author:「bollo」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/7456641/nan-error-in-ajax-callback

More about “NaN error in ajax callback” related questions

NaN error in ajax callback

I am getting a NaN error in my ajax callback function and can only think it has to do with an array in PHP. I have been trying to find ways to correct it but have come up against a brick wall. Wha...

Show Detail

Json data containing NaN results in error during ajax request

I an endpoint exposed via web api which spews out JSON like this: "[{"SomeId":1,"SomeName":"Some name 1","Parameter1":1.13,"Parameter2":3.0 ... to jquery ajax get requests. Everythin

Show Detail

Nan::TryCatch will not intercept exception thrown in Nan::Callback from JS code

Lets say I have a JS function which is simply throwing an error: function() { throw "Danger, Will Robinson!"; } This function passed in as an argument to a node.js addon and is used to construct ...

Show Detail

Jquery ajax error callback

I need some suggestions here or maybe some explanations. I have a jquery ajax call, $.ajax({ type: "GET", url: base_url+'/ajax/fetch/counts/', dataType: 'json', data: {}, error: function

Show Detail

Ajax: error callback not firing

I'm having trouble getting the error callback getting called when I pass the error function as an object parameter in a function. However, when I declare it within the ajax code it works. var

Show Detail

EXC_BAD_ACCESS: When calling Nan::Callback

I am writing a Node.js c++ addon which requires frequent callbacks from C++ to Javascript. The constructor requires two functions, an success and an error callback. ... // assuming info[0] and in...

Show Detail

Error callback in Ajax call

I am using a Jquery method to trigger an Ajax call. function fetchQuestion(questionId,index){ $.ajax({ url: '${createLink(action: 'fetchQuestion')}', data: {id:questionId,

Show Detail

Invoking some callback function twice leads to Segmentation fault: Nan

I am writing C++ addon using nbind - GitHub link for most thing and Nan - GitHub link for calling callbacks asynchronous. When I invoke callback only once, it works perfect. But When I invoke callb...

Show Detail

parametrize ajax callback

i'm using single ajax method all over my site and i need to parametrize all params and options, also callback methods (success,beforeSend,error, etc ..) for now i wrote: function ajax(_type,_url,...

Show Detail

how can i trigger ajax error callback on success callback?

I'm looking for a way to trigger ajax error in success callback. i have tried to explain my problem code below: $.ajax({ url: requestURL, data: postData, type: 'post', success: function(response){...

Show Detail