1
if($tag == 'signin'){
    // check for user
    $email = mysql_real_escape_string(trim($_POST['username']));
    $password = mysql_real_escape_string(trim($_POST['password']));

    if(isset($email) && !empty($email) && isset($password) && !empty($password)){
        $result = getUserByEmailAndPassword($email, $password);
        if ($result != false) {
            // user found
            $response["error"] = FALSE;
            $response['uid'] = $result['uid'];
            $response['email'] = $result['email'];
            $response['username'] = $result['username'];
            $response['password'] = $result['password'];
            $response['endyear'] = $result['endyear'];
            $response['phone'] = $result['phone'];
            $response['currentyear'] = $result['currentyear'];
            $response['currentsem'] = $result['currentsem'];
            echo json_encode($response);
        } else {
            // user not found
            // echo json with error = 1
            $response["error"] = TRUE;
            $response["error_msg"] = "Incorrect email or password!";
            header('Content-Type: application/x-www-form-urlencoded');
            echo json_encode($response);
        }           
    }else{
        $response["error"] = TRUE;
        $response["error_msg"] = "Required parameter 'username' or 'password' is missing!";
        echo json_encode($response);
    }
} 

I am using the above php code for my API

am I using header('Content-Type: application/x-www-form-urlencoded'); in the right place?

Also, when I check this request on my Advanced REST client in chrome, the request is just fine. the response is below:

{"tag":"signin","error":false,"uid":"10","email":"example@exmaple.com","username":"jammy","password":"40be4e59b9a2a2b5dffb918c0e86b3d7","endyear":null,"phone":null,"currentyear":null,"currentsem":null}

However, when I run the same thing on any online response checker, the response is all garbage HTML. Same with my application hosted online

I noticed that the chrome extension by default sets the Content-Type to application/x-www-form-urlencoded

The same code used to work fine when I was running my application on xampp on localhost, however, after I uploaded this online, I am facing this issue

Could someone pls help me figure what I am doing wrong

BountyHunter
  • 1,325
  • 15
  • 30

2 Answers2

1

It is not mandatory but if you plan to return JSON, the right header should be

header('Content-Type: application/json');

Otherwise, the receiver of the response may or may not interpret the body response as JSON.

Diego Ferri
  • 2,295
  • 2
  • 23
  • 30
0

I was able to locate the root cause of the problem. I made a lot of changes to the code only to realize later that this was due to MySQL DB tables

The tables I created on my local used InnoDB engine whereas my hosting provider provided on MyISAM engine.

All the tables created by importing the SQL file had engine set to MyISAM. I hosted the tables on a different provider with InnoDB engine and everything seemed to work fine.

Thank you everyone for trying to help

BountyHunter
  • 1,325
  • 15
  • 30