How to fix the jquery with ajax code localhost says object object error

3731 views jquery
0

When i call this jquery function with ajax getting this error

[object Object]

$('a#room_no').click(function() {
  var roomNumber = $(this).text();
  alert(roomNumber);
  var href = $(".room_check_out_form").attr('href');
  $.ajax({
    type: "POST",
    url: href,
    data: {
      roomNumber: roomNumber
    },
    dataType: "JSON",
    success: function(data) {
      console.log(data);
      alert(data.roomNumber)
    },
    error: function(err) {
      alert(err);
    }
  });
});

answered question

$('a#room_no').click(function() { var roomNumber = $(this).text(); alert(roomNumber); var href = $(".room_check_out_form").attr('href'); $.ajax({ type: "POST", url: href, data: {roomNumber:roomNumber}, dataType: "JSON", success: function(data) { console.log(data); alert(data.roomNumber) }, error: function(err) { alert(err); } }); });

Use console.log(...) instead of alert(...) and let us know what you see instead. I'd imagine you're getting an error and falling to alert(err), however alert() reveals no details about an object, whereas console.log() will.

1 Answer

3

In your error handler, err is an object; the alert(err) is causing '[object Object]' to be shown in an alert dialog.

Changing your error handler to something like this should log more useful information to the console. Inspect the properties of the err object:

error: function(err) {
    console.log(err);
    console.log(err.responseText);
    alert(err.responseText);
}

posted this

Have an answer?

JD

Please login first before posting an answer.