简体   繁体   English

在线托管时,来自PHP文件的JSON响应错误

[英]Error in JSON response from PHP file when hosted online

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 我正在使用上面的PHP代码作为我的API

am I using header('Content-Type: application/x-www-form-urlencoded'); 我是否在使用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. 另外,当我在Chrome的Advanced REST客户端上检查此请求时,该请求就很好了。 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. 但是,当我在任何联机响应检查器上运行相同的操作时,响应都是垃圾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 我注意到chrome扩展名默认将Content-Type设置为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 当我在本地主机上的xampp上运行应用程序时,相同的代码可以正常工作,但是,当我在线上传此代码后,我遇到了这个问题

Could someone pls help me figure what I am doing wrong 有人可以帮我弄清楚我做错了什么吗

It is not mandatory but if you plan to return JSON, the right header should be 这不是强制性的,但是如果您打算返回JSON,则正确的标头应为

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

Otherwise, the receiver of the response may or may not interpret the body response as JSON. 否则,响应的接收方可能会或可能不会将主体响应解释为JSON。

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 我对代码进行了很多更改,只是后来才意识到这是由于MySQL DB表引起的

The tables I created on my local used InnoDB engine whereas my hosting provider provided on MyISAM engine. 我在本地使用InnoDB引擎创建的表,而我的托管提供程序在MyISAM引擎上提供。

All the tables created by importing the SQL file had engine set to MyISAM. 通过导入SQL文件创建的所有表的引擎都设置为MyISAM。 I hosted the tables on a different provider with InnoDB engine and everything seemed to work fine. 我将表托管在具有InnoDB引擎的其他提供程序上,并且一切正常。

Thank you everyone for trying to help 谢谢大家的帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM