dongyin0628 2018-07-05 17:38
浏览 314

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

I am using Uploadcare Upload API and I am creating an app to upload photos and then add them to my localhost site. I keep getting back console error:

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

If anyone can find an error in my code and point me in the right direction, I would certainly appreciate it. The component ts code is as follows:

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

    }
  }
}

and the PHP is:

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

Placing console.log(data); before let json..., I get:

Response {_body: "<br />↵<b>Warning</b>:  Use of undefined constant …ngular-php\script.php</b> on line <b>9</b><br />↵", status: 200, ok: true, statusText: "OK", headers: Headers, …}

headers
:
Headers {_headers: Map(1), _normalizedNames: Map(1)}

ok
:
true

status
:
200

statusText
:
"OK"

type
:
2

url
:
"http://localhost/angular-php/script.php"

_body
:
"<br />↵<b>Warning</b>:  Use of undefined constant PUBLIC_KEY - assumed 'PUBLIC_KEY' (this will throw an Error in a future version of PHP) in <b>C:\xampp\htdocs\angular-php\script.php</b> on line <b>4</b><br />↵<br />↵<b>Notice</b>:  Undefined index: temp_name in <b>C:\xampp\htdocs\angular-php\script.php</b> on line <b>6</b><br />↵<br />↵<b>Warning</b>:  Use of undefined constant _FILE_ - assumed '_FILE_' (this will throw an Error in a future version of PHP) in <b>C:\xampp\htdocs\angular-php\script.php</b> on line <b>9</b><br />↵"

__proto__
:
Body
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 划分vlan后不通了
    • ¥15 GDI处理通道视频时总是带有白色锯齿
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)
    • ¥15 自适应 AR 模型 参数估计Matlab程序
    • ¥100 角动量包络面如何用MATLAB绘制
    • ¥15 merge函数占用内存过大
    • ¥15 使用EMD去噪处理RML2016数据集时候的原理
    • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大