1

I'm getting an error returned by the alert in the error:function, notice that I also have different console.logs to know where it's going. (In this case it's going 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);
              }
    });
});
/*
-1: PASSWORD NÃO COINCIDE COM A SUA CONFIRMAÇÃO
 0: EMAIL JÁ EXISTE NA BASE DE DADOS
 1: CONTA CRIADA COM SUCESSO
*/

});

I got this after doing what you guys said
from the alert:

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

from the echo:

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

Second Edit:

I checked the database and it seems its registering the values alright, I guess the error:function is called because of this:

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

Third Edit:

So I tried in another browser to see if I'd get the same response, this was the response from Microsoft Edge:

Microsoft Edge feedback:

enter image description here

3
  • so many possibilities, can be network/backend/php/ error. echo the return in the php and see what is returning Commented Feb 19, 2018 at 9:01
  • have you even try to console.log(data) ? Commented Feb 19, 2018 at 9:02
  • now I did, result= {readyState: 0, getResponseHeader: ƒ, getAllResponseHeaders: ƒ, setRequestHeader: ƒ, overrideMimeType: ƒ, …} Commented Feb 19, 2018 at 9:20

1 Answer 1

3

Your response is probably a js-object. You can't simply display a js-object in an alert

You could try using alert(JSON.stringify(data));

Sign up to request clarification or add additional context in comments.

5 Comments

Note: if you want to pretty print your JSON data, use JSON.stringify(data, null, 2);
Please don't call that a JSON object. It would be a JavaScript object. JSON is a text format. Or if you want to speak of a JSON object, it is the one you mention in your last line. Things get confusing otherwise. Otherwise put: JSON.stringify does not take JSON -- it produces it.
I got this: {readyState: 0, getResponseHeader: ƒ, getAllResponseHeaders: ƒ, setRequestHeader: ƒ, overrideMimeType: ƒ, …}
@dorinmunteanu Your alert is not in the success callback. I suggest you use console.log(data) to explore your object, and then use the property you need for your switch-case statement.
@Seblor how does that even work? I get different properties from different browsers, I don't think that's a solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.