Ionic + PHP:意外的令牌<在JSON位于JSON.parse的0位

时间:2018-01-22 22:35:06

标签: php json ionic-framework

我看到很多主题都有相同的错误,但我仍然无法解决我的问题...我不确定它有多具体,所以也许你可以帮助我。

这就是我的php文件:

if(isset($_POST)){

    $username = $_POST["username"];
    $email = $_POST["email"];
    $password = $_POST["password"];

    $sql = 'insert into users(username, email, password) values("' . $username . '", "' . $email . '", "' . $password . '")';

    if($conn->query($sql) === true){
        $output = "Inserted " . $username;

        echo json_encode($output);
    } else{
        echo json_encode("Error: " . $conn->error);
    }

} 

和我的提供者:

writeTable(u, e, p) : Promise<any>{

let url = "http://localhost/gameon-server-side/create.php";

let param = { username: u, email: e, password: p};

let request = this.http.post(url, param);

return request.toPromise();

}

我的模板:

<ion-row>
    <ion-col text-center>

        <p>Response:</p>
        <p>{{responseTxt}}</p>

        <button ion-button (click)="showTable()"> Read from table </button>

        <ion-input class="txt" type="text" placeholder="Enter Username" [(ngModel)]="userName"></ion-input>
        <ion-input class="txt" type="text" placeholder="Enter Email" [(ngModel)]="userEmail"></ion-input>
        <ion-input class="txt" type="text" placeholder="Enter Password" [(ngModel)]="userPassword"></ion-input>

        <button ion-button (click)="addTable(userName, userEmail, userPassword)"> Insert </button>
        <button ion-button> Update </button>
        <button ion-button> Delete </button>

    </ion-col>
</ion-row>

最后是我的组件:

addTable(u, e, p){
this.network.writeTable(u, e, p)
.then(data => {
  console.log("I received: " + JSON.stringify(data));
  this.responseTxt = ""+ JSON.stringify(data);
})
.catch(e => {
console.log(e);

})

}

和错误:

HttpErrorResponse {headers: HttpHeaders, status: 200, statusText: "OK", url: "http://localhost/gameon-server-side/create.php", ok: false, …}
error
:
error
:
SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>) at XMLHttpRequest.onLoad (http://localhost:8100/build/vendor.js:61165:37) at t.invokeTask (http://localhost:8100/build/polyfills.js:3:15660) at Object.onInvokeTask (http://localhost:8100/build/vendor.js:4973:33) at t.invokeTask (http://localhost:8100/build/polyfills.js:3:15581) at r.runTask (http://localhost:8100/build/polyfills.js:3:10834) at e.invokeTask [as invoke] (http://localhost:8100/build/polyfills.js:3:16794) at p (http://localhost:8100/build/polyfills.js:2:27648) at XMLHttpRequest.v (http://localhost:8100/build/polyfills.js:2:27893)
message
:
"Unexpected token < in JSON at position 0"
stack
:
"SyntaxError: Unexpected token < in JSON at position 0↵    at JSON.parse (<anonymous>)↵    at XMLHttpRequest.onLoad (http://localhost:8100/build/vendor.js:61165:37)↵    at t.invokeTask (http://localhost:8100/build/polyfills.js:3:15660)↵    at Object.onInvokeTask (http://localhost:8100/build/vendor.js:4973:33)↵    at t.invokeTask (http://localhost:8100/build/polyfills.js:3:15581)↵    at r.runTask (http://localhost:8100/build/polyfills.js:3:10834)↵    at e.invokeTask [as invoke] (http://localhost:8100/build/polyfills.js:3:16794)↵    at p (http://localhost:8100/build/polyfills.js:2:27648)↵    at XMLHttpRequest.v (http://localhost:8100/build/polyfills.js:2:27893)"
__proto__
:
Error
text
:
"<br />↵<b>Notice</b>:  Undefined index: username in <b>C:\xampp\htdocs\gameon-server-side\create.php</b> on line <b>25</b><br />↵<br />↵<b>Notice</b>:  Undefined index: email in <b>C:\xampp\htdocs\gameon-server-side\create.php</b> on line <b>26</b><br />↵<br />↵<b>Notice</b>:  Undefined index: password in <b>C:\xampp\htdocs\gameon-server-side\create.php</b> on line <b>27</b><br />↵"Inserted ""
__proto__
:
Object
headers
:
HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}
message
:
"Http failure during parsing for http://localhost/gameon-server-side/create.php"
name
:
"HttpErrorResponse"
ok
:
false
status
:
200
statusText
:
"OK"
url
:
"http://localhost/gameon-server-side/create.php"

干杯球员

3 个答案:

答案 0 :(得分:0)

您正在返回一个纯文本字符串($output = "Inserted " . $username;),然后尝试在javascript中将其解析为JSON。服务器可能正在响应Content-Type标头设置为text/html。您需要将其更改为application/json

请参阅PHP文档中的此页面:http://php.net/manual/en/function.header.php

虽然我真的建议使用某种REST框架(即Laravel),而不是手动配置所有响应。

答案 1 :(得分:0)

您发送的内容无效JSON

if($conn->query($sql) === true){
    $output = "Inserted " . $username;
    echo json_encode($output);
} else{
    echo json_encode("Error: " . $conn->error);
}

您应该将数组传递给json_encode以生成有效的JSON

至少在某些方面:

if($conn->query($sql) === true){
    $output = "Inserted " . $username;
    echo json_encode(['output' => $outpu]t);
} else{
    echo json_encode([error' => "Error: " . $conn->error]);
}

准备您的查询:

if(isset($_POST["email"], $_POST["username"], $_POST["password"])){
    $sql = 'insert into users(username, email, password) values (?, ?, ?)';
    if($stmt = $conn->prepare($sql)){
        $stmt->execute([$_POST["email"], $_POST["username"], $_POST["password"]]);
        $json = ['results' = > $stmt->rowCount()];
    } else{
        $json = ['error' => $con->error ]; 
    }
    echo json_encode($json);    
} 

答案 2 :(得分:0)

您的php响应没有正确返回JSON格式。要使用 json_encode ,您的变量必须是 ARRAY

$output = array();
if($conn->query($sql) === true){
    $output["inserted"] = $username;   
} else{
    $output["error"] = $conn->error;
}
echo json_encode($output);