尝试从api获取json数据,然后发布到站点Angular 6中

时间:2018-07-05 17:38:10

标签: php json angular6

我正在使用Uploadcare Upload API,并且正在创建一个应用程序来上传照片,然后将其添加到我的本地站点。我不断收到控制台错误:

>>> import pandas as pd
>>> s = pd.Series([True, False, False])
>>> s = s.apply(lambda x: 'B' if x else 'A')
>>> s
0    B
1    A
2    A
dtype: object

如果任何人都可以在我的代码中发现错误并为我指出正确的方向,我当然会感激的。 ts组件代码如下:

Unexpected token < in JSON at position 0
at JSON.parse (<anonymous>)
at Response.push../node_modules/@angular/http/fesm5/http.js.Body.json

PHP是:

import { Component, OnInit, Input } from '@angular/core';
import { Http } from '@angular/http';

import { GalleryComponent } from '../gallery/gallery.component';

@Component({
  selector: 'app-fileuploader',
  templateUrl: './fileuploader.component.html',
  styleUrls: ['./fileuploader.component.css']
})
export class FileuploaderComponent implements OnInit {

  ngOnInit() {}

  @Input()
    gallery: GalleryComponent;

  constructor(private http: Http) { }

  uploadFile(event) {
    let elem = event.target;
    if(elem.files.length > 0) {
      let formData = new FormData();
      formData.append('file', elem.files[0]);

      this.http.post('http://localhost/angular-php/script.php', formData).subscribe((data) => {

        let jsonResponse = data.json();

        this.gallery.gotSomeDataFromTheBackend(jsonResponse.file);
        console.log('Got some data from the backend ', data);
      }, (error) => {
        console.log('Error! ', error);
      });

    }
  }
}

放置console.log(data);在让json ...之前,我得到了:

<?php
header('Access-Control-Allow-Origin: *');
define(PUBLIC_KEY, 'cfcc5bbc8eb856a7d808');

$tempPath = $_FILES['file']['temp_name'];
$actualName = $_FILES['file']['name'];

$actualPath = dirname(_FILE_)."/temp/".$actualName;
move_uploaded_file($tempPath, $actualPath);


$ch = curl_init();

$post = [
  'UPLOADCARE_PUB_KEY'=>PUBLIC_KEY,
  'UPLOADCARE_STORE'=>1,
  'file'=> curl_file_create($actualPath)
];

curl_setopt($ch, CURLOPT_URL, 'https://upload.uploadcare.com/base/');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

$response = curl_exec($ch);

echo ($response);

0 个答案:

没有答案