如果我做邮递员,我会收到此回复
Array
(
[0] => stdClass Object
(
[product_id] => 5
[sku_id] => XYZ3
[name] => Kanchipuram Silk
[price] => 3500
[category_id] => 2
[description] => Etiam faucibus viverra libero vel efficitur. Ut semper nisl ut laoreet ultrices. Maecenas dictum arcu purus, sit amet volutpat purus viverra sit amet. Quisque lacinia quam sed tortor interdum, malesuada congue nunc ornare. Cum sociis natoque penatibus et magnis dis parturient montes
[popular_status] => 0
[active_status] => 1
)
[1] => stdClass Object
(
[product_id] => 4
[sku_id] => XYZ1
[name] => Kanchipuram Silk
[price] => 999
[category_id] => 2
[description] => Etiam faucibus viverra libero vel efficitur. Ut semper nisl ut laoreet ultrices. Maecenas dictum arcu purus, sit amet volutpat purus viverra sit amet. Quisque lacinia quam sed tortor interdum, malesuada congue nunc ornare. Cum sociis natoque penatibus et magnis dis parturient montes
[popular_status] => 1
[active_status] => 1
)
[2] => stdClass Object
(
[product_id] => 3
[sku_id] => ABC3
[name] => Kanchipuram Silk
[price] => 2530
[category_id] => 1
[description] => Etiam faucibus viverra libero vel efficitur. Ut semper nisl ut laoreet ultrices. Maecenas dictum arcu purus, sit amet volutpat purus viverra sit amet. Quisque lacinia quam sed tortor interdum, malesuada congue nunc ornare. Cum sociis natoque penatibus et magnis dis parturient montes
[popular_status] => 0
[active_status] => 1
)
[3] => stdClass Object
(
[product_id] => 2
[sku_id] => ABC2
[name] => Kanchipuram Silk
[price] => 1525
[category_id] => 1
[description] => Etiam faucibus viverra libero vel efficitur. Ut semper nisl ut laoreet ultrices. Maecenas dictum arcu purus, sit amet volutpat purus viverra sit amet. Quisque lacinia quam sed tortor interdum, malesuada congue nunc ornare. Cum sociis natoque penatibus et magnis dis parturient montes
[popular_status] => 1
[active_status] => 1
)
)
{"0":{"product_id":"5","sku_id":"XYZ3","name":"Kanchipuram Silk","price":"3500","category_id":"2","description":"Etiam faucibus viverra libero vel efficitur. Ut semper nisl ut laoreet ultrices. Maecenas dictum arcu purus, sit amet volutpat purus viverra sit amet. Quisque lacinia quam sed tortor interdum, malesuada congue nunc ornare. Cum sociis natoque penatibus et magnis dis parturient montes","popular_status":"0","active_status":"1"},"1":{"product_id":"4","sku_id":"XYZ1","name":"Kanchipuram Silk","price":"999","category_id":"2","description":"Etiam faucibus viverra libero vel efficitur. Ut semper nisl ut laoreet ultrices. Maecenas dictum arcu purus, sit amet volutpat purus viverra sit amet. Quisque lacinia quam sed tortor interdum, malesuada congue nunc ornare. Cum sociis natoque penatibus et magnis dis parturient montes","popular_status":"1","active_status":"1"},"2":{"product_id":"3","sku_id":"ABC3","name":"Kanchipuram Silk","price":"2530","category_id":"1","description":"Etiam faucibus viverra libero vel efficitur. Ut semper nisl ut laoreet ultrices. Maecenas dictum arcu purus, sit amet volutpat purus viverra sit amet. Quisque lacinia quam sed tortor interdum, malesuada congue nunc ornare. Cum sociis natoque penatibus et magnis dis parturient montes","popular_status":"0","active_status":"1"},"3":{"product_id":"2","sku_id":"ABC2","name":"Kanchipuram Silk","price":"1525","category_id":"1","description":"Etiam faucibus viverra libero vel efficitur. Ut semper nisl ut laoreet ultrices. Maecenas dictum arcu purus, sit amet volutpat purus viverra sit amet. Quisque lacinia quam sed tortor interdum, malesuada congue nunc ornare. Cum sociis natoque penatibus et magnis dis parturient montes","popular_status":"1","active_status":"1"},"status":"200"}
我尝试使用组件
来获取此内容import { Component, OnInit } from '@angular/core';
import { Http, Response } from '@angular/http';
import 'rxjs/add/operator/map';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {
productCount: number = 4;
private apiUrl = 'http://localhost/sp/api/unsold_products';
products: any;
constructor(private http: Http) {
this.getProducts();
}
getData() {
return this.http.get(this.apiUrl)
.map((res: Response) => res.json())
}
getProducts(){
this.getData().subscribe(data => {
console.log(data);
this.products = data;
})
}
ngOnInit() {
}
}
但现在我在控制台中收到此错误。我认为错误可能是由于我从服务器获得的一些html内容。但是邮递员的一切都很好但不在这里。
ERROR SyntaxError: Unexpected token A in JSON at position 0
at JSON.parse (<anonymous>)
我使用angularjs 5进行编码,使用codeigniter框架进行服务器端I编码。这是我的CI控制器
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Api extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->model('Api_model');
}
public function unsold_products(){
$response = $this->Api_model->fetch_unsold_products();
print_r($response);
$response['status']='200';
json_output($response['status'],$response);
}
}
我没有到达我出错的地方。
答案 0 :(得分:1)
只能从服务器返回JSON。您已使用print_r()在服务器上打印数据。这将导致这些类型的输出。从代码中删除print_r($ response)并检查输出。
答案 1 :(得分:0)
我自己找到了解决方案。 我添加了
header('Content-Type: application/json');
在控制器的第一行。这是完整的控制器:
<?php
header('Content-Type: application/json');
class Api extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->model('Api_model');
$this->load->model('Generic_model');
}
}
希望这可能对某人有所帮助..