简体   繁体   English

Ionic + PHP:意外的令牌<JSON中的JSON.parse位置0

[英]Ionic + PHP: Unexpected token < in JSON at position 0 at JSON.parse

I have seen loads of topics with the same error, but I still couldn't fix my issue... I'm not sure how specific it is, so maybe you can help me. 我看到了很多带有相同错误的主题,但是我仍然无法解决我的问题...我不确定它的具体程度,因此也许您可以帮助我。

This is what my php file looks like: 这是我的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);
    }

} 

and my provider: 和我的提供者:

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();

} }

my template: 我的模板:

<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>

and finally my component: 最后是我的组件:

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);

}) })

} }

and the error: 和错误:

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"

Cheers guys 欢呼的家伙

You are returning a plain string of text ( $output = "Inserted " . $username; ) and then trying to parse it as a JSON in your javascript. 您将返回纯文本字符串( $output = "Inserted " . $username; ),然后尝试在JavaScript中将其解析为JSON。 The server is probably responding with the Content-Type header set to text/html . 服务器可能正在响应,将Content-Type标头设置为text/html You'll need to change it to application/json . 您需要将其更改为application/json

See this page in the PHP documentation: http://php.net/manual/en/function.header.php 参见PHP文档中的此页面: http : //php.net/manual/zh/function.header.php

Though I really recommend using a REST framework of some kind (ie Laravel) rather than manually configuring all your responses. 尽管我确实建议使用某种REST框架(即Laravel),而不是手动配置所有响应。

What you are sending is not valid JSON : 您发送的内容无效JSON

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

you should pass an array to json_encode to produce valid JSON 您应该将数组传递给json_encode以生成有效的JSON

At least something along the lines: 至少是这样:

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

prepare your query: 准备查询:

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);    
} 

Your php response didnt return JSON format correctly.To use json_encode your variable must be an ARRAY 您的php响应未正确返回JSON格式。要使用json_encode,您的变量必须为ARRAY

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

暂无
暂无

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

相关问题 我正在使用Ionic并收到错误消息:SyntaxError:JSON中的意外标记&lt;在JSON.parse位置0处( <anonymous> ) - I am using ionic and getting an error: SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>) JSON中的意外令牌&lt;在JSON.parse位置0处( <anonymous> ) - Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>) SyntaxError: JSON JSON JSON.parse 0 中的意外标记 - SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse 未捕获到的SyntaxError:JSON中的意外令牌&lt;在JSON.parse位置0处( <anonymous> ) - Uncaught SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>) SyntaxError:JSON中的意外令牌&lt;位于JSON.parse(位置0) <anonymous> )-AngularJS - SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>) - AngularJS 未捕获的语法错误:JSON 中的意外令牌 < position 中的 0 JSON.parse - Wordpress - Uncaught SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse - Wordpress JSON.parse意外令牌多维数组 - JSON.parse unexpected token multidimensional array jQuery.Deferred exception: Unexpected token &lt; in JSON at position 0 SyntaxError: Unexpected token &lt; in JSON at position 0 at JSON.parse (<anonymous> )</anonymous> - jQuery.Deferred exception: Unexpected token < in JSON at position 0 SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>) 显示SyntaxError的json响应:意外令牌[JSON中JSON.parse( <anonymous> ) - json response showing SyntaxError: Unexpected token [ in JSON at position 23 at JSON.parse (<anonymous>) JSON中位置Ionic处的意外令牌&lt; - Unexpected token < in JSON at position Ionic
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM