When using $.ajax, an Object Object is returned

Encountering an error from the alert in the error function, with additional console.logs for debugging purposes. (Specifically tracking it entering into the error function)


$( document ).ready(function() {
console.log( "ready!" );

$('#register').click(function(){
    $.ajax({
        url: "./php/register.php", 
        type: 'POST', 
        dataType: 'json',
        data: {
            uname: $('#uname').val(),
            usur: $('#usur').val(),
            umail: $('#umail').val(),
            psw: $('#psw').val(),
            conf_psw: $('#conf_psw').val()
        },
              success:function(data){
                switch(data){
                    case "-1":
                    $('#content').load("./register.html");
                    break;

                    case "0":
                    $('#content').load("./register.html");
                    break;

                    case "1":
                    $('#content').load("./login.html");
                    break;
                }
                console.log("0");
                alert(data);
              },
              error:function(data){
                console.log("-1");
                alert(data);
              }
    });
});

});

After following suggestions, encountered this message:
from the alert:

{"readyState":0,"status":0,"statusText":"error"}

Received this response from the echo output:

{readyState: 0, getResponseHeader: ƒ, getAllResponseHeaders: ƒ, setRequestHeader: ƒ, overrideMimeType: ƒ, …}

Further Investigation:

Database entries appear to be successful, possibly triggering the error function due to:

https://stackoverflow.com/questions/44515506/ajax-why-do-i-get-an-error-function-instead-of-success

Alternative Trial:

Tested on a different browser for consistency, here is the result from Microsoft Edge:

Microsoft Edge feedback:

https://i.sstatic.net/1yQvV.png

Answer №1

It is likely that the content you are dealing with is a JavaScript object. Displaying a JavaScript object directly in an alert message is not possible.

One alternative solution is to utilize alert(JSON.stringify(data));

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Node.js - Request timeout issue: Headers cannot be set after they have already been sent

My node.js app is running with mongoose, express, mongodb. I have a 'team' page that displays teams from the database. Everything was working fine until I added the code below. Now, when I navigate to the page and refresh it or try to load it aga ...

What could be causing my React state to not update correctly?

I've been working on integrating the Firebase authentication feature into a React project. The code snippet below shows the component responsible for managing the authentication state. import { useState} from "react"; function useAuth(fbAu ...

The controller is unable to read the property 'goals' as it is undefined in AngularJS resolve

Hello everyone, this is my first post on stackoverflow. I have encountered an issue in my code that is causing an error which reads like this: TypeError: Cannot read property 'goals' of undefined at $scope.addValue.$modal.open.resolve.g ...

The typical number of keys in a ChartJS graph is 2

Currently, I am utilizing ChartJS for a project that displays user-added records such as weight and heart rate for tracking purposes. I have encountered an issue where if a user adds two slightly different value records for the same day, it results in both ...

Obtaining the text content of a <div> element when clicked using Selenium

I am trying to extract the email address from the code snippet below, but I am unsure of how to do it. Any assistance would be greatly appreciated! <div class="taLnk hvrIE6 fl" onclick="ta.trackEventOnPage('Listing', 'Email', 774169 ...

Initiate a click event on an anchor element with the reference of "a href"

I'm currently facing an issue with clicking a href element on my HTML website. The click event doesn't seem to be triggered. CODE: HTML CODE: <div class="button" id="info" ></div> <ul id="game-options"> <li><a ...

Restrict the character count in tinyMCE

My project uses the tinyMCe editor and everything is functioning properly. However, I now need to limit the number of characters that can be inserted into the tinyMce textarea. tinyMCE.init({ // General options mode : "textareas", theme : "simple", plugin ...

How can I delay the execution of "onAuthStateChanged" until "currentUser.updateProfile" has completed?

Currently facing an issue with registering users in my Vue app. When a user registers, I need to trigger the updateProfile function to add more user data. However, the problem arises when the onAuthStateChanged function in my main.js is executed before the ...

Guide to excluding all subdependencies using webpack-node-externals

My current setup involves using webpack to bundle both server assets and client code by specifying the target property. While this configuration has been working well, I encountered an issue where webpack includes all modules from node_modules even for ser ...

What is the best way to implement a keypress event in this code?

As a JavaScript and jQuery newbie, I have a question regarding adding a simple key press action to this code. How can I implement functionality where pressing the Right Arrow key takes me to the next image, and pressing the Left Arrow key takes me to the p ...

No longer able to bind events after making changes to SVG elements [using JSFiddle]

I've coded a personalized directive to display tags like `<rect>` and `<circle>` within `<svg>` elements. My shapes model is simply an array of objects with different attributes such as `width`, `height`, and `stroke-width. These s ...

Leveraging the power of javascript to include content before and after

I am looking to understand how I can insert an HTML element before and after certain elements. For example, let's say we have the following code in a real file: <ul class="abcd" id="abcd></ul> How can I display it like this using JavaScr ...

Is there a way to access request or response objects in express.Js from any location?

Within an express.Js application, I aim to create a controller class responsible for managing the request and response for various other controllers. This includes tasks such as adding data to locals in the res object or deleting data from req.session. I ...

There seems to be an issue with the Google reCAPTCHA login, as it is displaying an error with the code 'invalid-input-secret'

Customer Interface: grecaptcha.ready(function() { grecaptcha.execute('6Le4oroZABBXXIQCQkAYCXYSekNQnWExTeNUBZ-B', {action: 'submit'}).then(function(token) { $scope.userData['repatcha_token'] = token $ht ...

Using CoffeeScript to Invoke Methods

I'm still learning CoffeeScript and encountering some challenges with calling methods. Can someone help me out? Here is the Card model I am working with: class exports.Card extends Backbone.Model defaults: pip: '4' suit: &apos ...

Perform two ng-repeat loops using both odd and even conditions

I am working with 2 tables where I need to display odd and even records using ng-repeat. The requirement is to showcase odd records first, followed by even records in the table. For Table 1 tag: implement odd logic For Table 2 tag: implement even logic ...

How to send Multipart form data with a string payload

Many suggestions in regards to this issue recommend utilizing some form of FormData within nodejs for building a multipart form. However, I am seeking to achieve the same result without relying on the FormData library. Instead, I aim to use only request h ...

In order to effectively manage the output of these loaders, it may be necessary to incorporate an extra loader. This can be achieved by using the

I'm currently working with react typescript and trying to implement a time zone picker using a select component. I attempted to utilize the npm package react-timezone-select, but encountered some console errors: index.js:1 ./node_modules/react-timezo ...

Retrieving text data in Controller by utilizing jQuery AJAX request

Text box and button for input <input type="text" class="form-control" name="ClaimNumber" placeholder="Enter a claim number" id="ClaimNumber" /> <button class="btn btnNormal" type="submit" id="btnSearch"> ...

Guide to organizing an array of objects based on the month and year they belong to?

I currently have an array of objects structured as follows: entries: [ { id:1, text: "Lorem ipsum", timeStamp:"Thu, 01 Jun 2018" }, { id:3, text:"Lorem ipsum", timeStamp:"Thu, 24 May 2018" }, { ...