SyntaxError:意外的令牌<在JSON的位置1离子

时间:2018-02-17 10:57:52

标签: json angular ionic-framework

我该如何解决这个问题?请你帮助我好吗。谢谢。 我遇到了一个类似的错误,结果是渲染函数中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);
      });

    });

  }

}

signup.ts

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>

2 个答案:

答案 0 :(得分:0)

100%此错误是由于api的错误响应,请将您的api响应检查到日志中

答案 1 :(得分:0)

从api文件夹的public static void saveImage(Bitmap bm, String fileName,boolean needCompress) { try { File myCaptureFile = new File(fileName); File parent = myCaptureFile.getParentFile(); if (!parent.exists()) { parent.mkdirs(); } BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile)); if(needCompress){ bm.compress(Bitmap.CompressFormat.JPEG, 85, bos); }else{ bm.compress(Bitmap.CompressFormat.PNG, 100, bos); } bos.flush(); bos.close(); } catch (IOException e) { throw new RuntimeException("IOException occurred when saving image: " + fileName, e); } } 中,删除或注释此行“ index.php”在83位置 而此行“ echo $email_check.'<br/>'.$email;”位于87位置

理由: echo ' Here';文件中localStorage.setItem的第二个参数必须是JSON格式 但是api发送

signup.ts

这不是json格式

对我来说很好