簡體   English   中英

SyntaxError:JSON中位置1 ionic上的意外令牌<

[英]SyntaxError: Unexpected token < in JSON at position 1 ionic

我怎么解決這個問題? 請你幫助我好嗎。 謝謝。 我遇到了一個類似的錯誤,事實證明這是render函數中HTML的錯字,但這里似乎並非如此。

更令人困惑的是,我將代碼回滾到了較早的已知工作版本,但仍然出現錯誤。

錯誤信息;

SyntaxError: Unexpected token < in JSON at position 1
        at JSON.parse (<anonymous>)
        at Response.Body.json (http://localhost:8100/build/vendor.js:67304:25)
        at SafeSubscriber._next (http://localhost:8100/build/main.js:358:29)
        at SafeSubscriber.__tryOrUnsub (http://localhost:8100/build/vendor.js:32821:16)
        at SafeSubscriber.next (http://localhost:8100/build/vendor.js:32768:22)
        at Subscriber._next (http://localhost:8100/build/vendor.js:32708:26)
        at Subscriber.next (http://localhost:8100/build/vendor.js:32672:18)
        at XMLHttpRequest.onLoad (http://localhost:8100/build/vendor.js:67797:38)
        at t.invokeTask (http://localhost:8100/build/polyfills.js:3:15660)
        at Object.onInvokeTask (http://localhost:8100/build/vendor.js:4973:33)

提供者/auth-service.ts

import { Injectable } from '@angular/core';
import { Http, Headers } from '@angular/http';
import 'rxjs/add/operator/map';

let apiUrl = "http://localhost/PHP-Slim-Restful/api/";


@Injectable()
export class AuthServiceProvider {

  constructor(public http: Http) {
    console.log('Hello AuthService Provider');
  }

  postData(credentials, type){

    return new Promise((resolve, reject) =>{
      let headers = new Headers();
      this.http.post(apiUrl+type, JSON.stringify(credentials), {headers: headers}).
      subscribe(res =>{
        resolve(res.json());
      }, (err) =>{
        reject(err);
      });

    });

  }

}

注冊

import { Component } from '@angular/core';
    import { IonicPage, NavController, NavParams } from 'ionic-angular';
    import { TabsPage } from '../tabs/tabs';
    import { LoginPage } from '../login/login';
    import { AuthServiceProvider } from '../../providers/auth-service/auth-service';
    import 'rxjs/add/operator/map';

@IonicPage()
@Component({
  selector: 'page-signup',
  templateUrl: 'signup.html',
})
export class SignupPage {
  responseData: any;
  userData = { "username": "", "password": "", "email": "", "name": "" };
  constructor(
    public navCtrl: NavController,
    public navParams: NavParams,
    public authService: AuthServiceProvider) {
  }

  ionViewDidLoad() {

  }
  signup() {
    //api connections

    this.authService.postData(this.userData, "signup")
      .then((result) => {
        this.responseData = result;
        console.log(this.responseData);
        localStorage.setItem('userData', JSON.stringify(this.responseData))
        this.navCtrl.push(TabsPage);
      }, (err) => {

        //connection failed message
      });
  }
  login() {
    this.navCtrl.push(LoginPage);
  }
}

注冊html

<!--
  Generated template for the SignupPage page.

  See http://ionicframework.com/docs/components/#navigation for more info on
  Ionic pages and navigation.
-->
<ion-header>

  <ion-navbar>
    <ion-title>Kayıt OL</ion-title>
  </ion-navbar>

</ion-header>


<ion-content padding>
  <ion-list>
    <ion-item>
      <ion-label floating>Adınız</ion-label>
      <ion-input type="text" [(ngModel)]="userData.name"></ion-input>
    </ion-item>
    <ion-item>
      <ion-label floating>Mail Adresiniz</ion-label>
      <ion-input type="text" [(ngModel)]="userData.email"></ion-input>
    </ion-item>
    <ion-item>
      <ion-label floating>Kullanıcı Adı</ion-label>
      <ion-input type="text" [(ngModel)]="userData.username"></ion-input>
    </ion-item>

    <ion-item>
      <ion-label floating>Şifre</ion-label>
      <ion-input type="password" [(ngModel)]="userData.password"></ion-input>
    </ion-item>

  </ion-list>

  <div padding>
    <button ion-button block (click)="signup()">Üye Ol</button>
  <a href="#" (click)="login()">Login Page</a>
  </div>
</ion-content>

100%此錯誤是由於api響應錯誤引起的,請檢查您的api響應到日志中

從api文件夾的index.php ,只需刪除或注釋此行“ echo $email_check.'<br/>'.$email; ”在83位置,此行“ echo $email_check.'<br/>'.$email; echo ' Here'; ”在87位置

理由: signup.ts文件中localStorage.setItem的第二個參數必須為JSON格式,但api發送

"<br/>email@gmail.comhere{"userData": {"user_id":"4","name":"My Name here",
"email":email@gmail.com","username":"myusernamehere",
"token":"c0045c0d75e48f62919b1c0d16461af854049615df2c0d9a7bd755e9679e662a"}}" 

這不是json格式

對我來說很好

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM