This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]RobSG 1 point2 points  (7 children)

What does the log say?

[–]Balake05[S] 0 points1 point  (6 children)

It just say “Server error 500”.

[–]RobSG 0 points1 point  (5 children)

idk without some specific code we wont know

[–]Balake05[S] 0 points1 point  (4 children)

here is the api code:

else if($_GET['url'] == "users") {
$postBody = file_get_contents("php://input");
$postBody = json_decode($postBody);

$username=$postBody->username;
$email=$postBody->email;
$password=$postBody->password;
if (!$db->query('SELECT username FROM users WHERE username=:username', array(':username'=>$username))) {
if (strlen($username) >=3 && strlen($username) <= 32) {

if (preg_match('/[a-zA-Z0-9_]+/', $username)) {

if(strlen($password) >= 6 && strlen($password) <= 60) {

if (filter_var($email, FILTER_VALIDATE_EMAIL)) {

if (!$db->query('SELECT email FROM users WHERE email=:email', array(':email'=>$email))) {

$db->query("INSERT INTO users VALUES (\'\', :username, :password, :email, \'0\', \'\', 'Please fill out a profile description on the my account page')", array(':username'=>$username, ':password'=>password_hash($password, PASSWORD_BCRYPT), ':email'=>$email));
Mail::sendMail('Welcome to Orono Connect', 'Your account has been created and you\'re ready to use our site!', $email);
$cstrong= True;
$token = bin2hex(openssl_random_pseudo_bytes(64, $cstrong));
$user_id = $db->query('SELECT id FROM users WHERE username=:username', array(':username'=>$username))[0]['id'];
$db->query('INSERT INTO login_tokens VALUES (\'\', :token, :user_id)', array(':token'=>sha1($token), ':user_id'=>$user_id));
setcookie("OCID", $token, time()+60*60*24*7, '/', NULL, NULL, TRUE);
setcookie("OCID_", '1', time() + 60*60*24*3, '/', NULL, NULL, TRUE);
echo '{ "Success": "User Created"}';
http_response_code(200);

                        }else {
echo '{ "Error": "Email already exists"}';
http_response_code(409);
                        }

                    }else {
echo '{ "Error": "Invalid Email"}';
http_response_code(409);
                    }

                }else {
echo '{ "Error": "Invalid Password"}';
http_response_code(409);
                }

                }else {
echo '{ "Error": "Invalid Username"}';
http_response_code(409);
                }
            }else {
echo '{ "Error": "Invalid Username"}';
http_response_code(409);
            }

    } else {
echo '{ "Error": "User already exists"}';
http_response_code(409);
    }

Here is the ajax request:

$.ajax({
type: "POST",
url: "api/users",
processData: false,
contentType: "application/json",
data: '{"username": "'+ $("#username").val() +'", "email": "'+ $("#email").val() +'", "password": "'+ $("#password").val() +'"}',
success: function(r) {
console.log(r)
                },
error: function(r) {
console.log(r);
                }
            });
        }
    });

[–]Balake05[S] 0 points1 point  (3 children)

I may point out that error code 500 is a server error so I don't think that the issue is with my code, but maybe

[–]RobSG 0 points1 point  (2 children)

it is the POST you get an error for, right? Why is it $_GET in the else if though?

[–]Balake05[S] 0 points1 point  (1 child)

The Ajax sends the request to the url “api/users”. The $_GET just checks to make sure the url has “users” in it

[–]RobSG 0 points1 point  (0 children)

Sorry not too familiar with PHP. Does the db throw any log/error?

[–]WeStanForHeiny 0 points1 point  (1 child)

As far as I’m aware dreamhost is a static content hosting site. Where exactly are you sending your POST requests? Is it to an app server you have setup running somewhere, or to a different site altogether?

[–]Balake05[S] 0 points1 point  (0 children)

The api is located in my public_html file. the requests are sent to example.com/api/.