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 lammps拉伸应力应变曲线分析
    • ¥15 C++ 头文件/宏冲突问题解决
    • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
    • ¥50 安卓adb backup备份子用户应用数据失败
    • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
    • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
    • ¥30 python代码,帮调试
    • ¥15 #MATLAB仿真#车辆换道路径规划
    • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
    • ¥15 数据可视化Python