【发布时间】:2018-10-22 11:13:36
【问题描述】:
我只是想让 php 文件“回显 $string = 'hello world';”在我的 ReactJS 应用程序的 html 页面上。
但我从浏览器(谷歌浏览器)收到此错误:
“未处理的拒绝(SyntaxError):JSON 中位置 0 处的意外标记 h”
表明这是导致问题的行:
.then((response) => response.json()
我的 JavaScript:
import React, { Component } from 'react'
import './App.css'
class App extends Component {
componentDidMount(){
fetch('http://localhost/finaldemo.php')
.then((response) => response.json())
.then((responseJson) => {
console.log(responseJson)
})
}
render () {
return (
<div>
<p> HELLO </p>
</div>
);
}
}
export default App;
我的 PHP:
<?php header('Access-Control-Allow-Origin: http://localhost:3000');?>
<?php
echo $string = 'hello world';
?>
问题出在哪里?
提前致谢!
【问题讨论】:
-
hello world不是有效的 json。 -
检查您的响应标头。它是
Content-Type:application/json或其他。
标签: javascript php html json reactjs