我在React Web应用程序中使用Axios进行API调用.但是我在Chrome中收到错误,
XMLHttpRequest无法加载https://example.restdb.io/rest/mock-data.请求的资源上不存在"Access-Control-Allow-Origin"标头.因此,不允许来源" http:// localhost:8080 "访问.
{
axios.get('https://example.restdb.io/rest/mock-data', {
headers: {
'x-apikey': 'API_KEY',
},
responseType: 'json',
}).then(response => {
this.setState({ tableData: response.data });
});
}
Run Code Online (Sandbox Code Playgroud)
我还在StackOverflow上读了几个关于同一问题的答案,标题为"Access-Control-Allow-Origin",但仍然想弄清楚如何解决这个问题.我不想使用扩展程序IN Chrome或使用临时黑客来解决这个问题.请提出解决上述问题的标准方法.
在尝试了几个答案后,我尝试了这个,
headers: {
'x-apikey': '59a7ad19f5a9fa0808f11931',
'Access-Control-Allow-Origin' : '*',
'Access-Control-Allow-Methods' : 'GET,PUT,POST,DELETE,PATCH,OPTIONS',
},
Run Code Online (Sandbox Code Playgroud)
现在我收到错误了,
请求标头字段Access-Control-Allow-Origin在预检响应中不允许使用Access-Control-Allow-Origin
我在React项目中使用Material UI Next.我有卡组件,里面有图像(卡片介质)和文本(卡片文本).我的文字下方还有一个按钮.我的问题是..如何使整张卡片可点击?即.无论用户是否按下卡片文本,卡片图像或按钮,都应触发我在按钮上调用的onClick事件.
我是iOS开发人员,目前正在开发一个实验性的React Native应用程序.我有以下代码,在屏幕上显示一个按钮和示例文本.
import React from 'react';
import { StyleSheet, Text, View , Button } from 'react-native';
export default class App extends React.Component {
constructor() {
super();
this.state = {sampleText: 'Initial Text'};
}
changeTextValue = () => {
this.setState({sampleText: 'Changed Text'});
}
_onPressButton() {
<Text onPress = {this.changeTextValue}>
{this.state.sampleText}
</Text>
}
render() {
return (
<View style={styles.container}>
<Text onPress = {this.changeTextValue}>
{this.state.sampleText}
</Text>
<View style={styles.buttonContainer}>
<Button
onPress={this._onPressButton}
title="Change Text!"
color="#00ced1"
/>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: …Run Code Online (Sandbox Code Playgroud) 我试图使用CLI在Heroku上部署React Web应用程序.但是当我跑的时候,
git push heroku master
Run Code Online (Sandbox Code Playgroud)
从我的项目文件夹中它会抛出一个错误:
Counting objects: 213, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (212/212), done.
Writing objects: 100% (213/213), 515.89 KiB | 0 bytes/s, done.
Total 213 (delta 40), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Node.js app detected
remote:
remote: -----> Build failed
remote: ! Two different lockfiles found: package-lock.json and
yarn.lock
remote:
remote: Both npm and yarn have created lockfiles for this …Run Code Online (Sandbox Code Playgroud) 编辑:我已经在SO上阅读了相同问题的其他答案,但我无法获得所需的输出.我尝试过其他问题中提出的许多变化,但它不起作用.
我有一个JSON剪切,当我打开websocket时需要将其添加为正文.
sender: "system1@example.com",
recipients:"system2@example.com",
data: {
text: "Test Message"
},
Run Code Online (Sandbox Code Playgroud)
所以使用Swift我做了以下,
var messageDictionary : [String: Any] = [
"sender": "system1@example.com",
"recipients":"system2@example.com",
"data": [
"text": "Test Message"
],
]
do {
let jsonData = try JSONSerialization.data(withJSONObject: messageDictionary, options: .prettyPrinted)
let jsonString = String(data: jsonData, encoding: String.Encoding.ascii)
socket.write(string: jsonString!)
print(jsonString)
} catch {
print(error.localizedDescription)
}
Run Code Online (Sandbox Code Playgroud)
当我打印jsonString时,我明白了
Optional("{\n \"sender\" : \"system1@example.com\",\n \"data\" : {\n
\"text\" : \"Test Message\"\n },\n \"recipients\" :
\"system2@example.com\"\n}")
Run Code Online (Sandbox Code Playgroud)
作为控制台输出.我希望上面的代码片段格式化为JSON.如何在没有/ n和其他空格的情况下将输出作为普通JSON?我使用Swift 4和Xcode 9.1
编辑2:
let jsonData = try …Run Code Online (Sandbox Code Playgroud) 我有一个使用 React Table( https://react-table.js.org )实现的表,功能非常快速并显示数据,但它有自己的默认样式。我想在上面实现 Material design 主题。通向它的方法是什么?我应该包含一个 CSS 文件吗?
我是React的新手,我必须从API中获取数据并将其显示在表格中.我使用React Table来显示表中的数据.如何实现以上?目前我没有在Google Chrome开发者控制台中看到服务器的任何响应.React Table实现使用本地数据,但是从API填充表是行不通的.我的代码如下:
class TableExp extends React.Component {
constructor() {
super();
this.state = {
tableData: {
resourceID: '',
resourceType: '',
tenantName: '',
dealerID: '',
status: '',
logFilePath: '',
supportPerson: '',
lastUpdatedTime: '',
},
};
}
componentDidMount() {
axios.get(`https://myAPI.restdb.io/rest/mock-data`, {
headers: {'x-apikey': 'apiKEY'}
})
.then(response => {
this.setState({ tableData: response.data.tableData });
//console.log(tableData);
});}
render() {
const { tableData } = this.state;
return (
<div>
<ReactTable
data={tableData}
columns={[
{
Header: 'Details',
columns: [
{
Header: 'Tenant Name',
accessor: '{this.state.tableData.tenantName}',
},
{ …Run Code Online (Sandbox Code Playgroud) 我使用React Table(http://react-table.js.org)在页面中显示一个表,并使用从API调用的数据填充它.我想将其中一列显示的值作为链接(一个hrefs).此特定列仅包含URL.如何在React Table中实现它?
columns: [
{
filterable: false,
Header: 'Click here',
accessor: 'link',
render: e => <a href={e.value}> {e.value} </a>,
},
],
Run Code Online (Sandbox Code Playgroud)
我将"e"作为正在表中显示的数据并将其包装在ahref中以将其转换为链接.但是,这种方法不起作用.
我正在使用React Table来显示我从API调用接收到的数据。目前,我将状态保存在本地,该表显示数据。我添加了两列的列值过滤。我的过滤逻辑如下:
<ReactTable
data={tableData}
noDataText="Loading.."
filterable
defaultFilterMethod={(filter, row) =>
String(row[filter.id]) === filter.value}
};
}}
columns={[
{
columns: [
{
Header: 'Name',
accessor: 'Name',
id: 'Name',
Cell: ({ value }) => (value === 'group1' ?
'group1' : 'group2'),
filterMethod: (filter, row) => {
if (filter.value === 'all') {
return true;
}
if (filter.value === 'group1') {
return row[filter.id] === 'group1';
}
if (filter.value === 'group2') {
return row[filter.id] === 'group2';
}
},
Filter: ({ filter, onChange }) =>
<select
onChange={event => …Run Code Online (Sandbox Code Playgroud) 我有一个环境变量,我需要在我的render方法中访问.由于它是一个自定义的ENV变量,我不能使用(process.env.NODE_ENV).我已经读过React清理所有process.env访问权限.
如何在React Web应用程序中访问我的自定义环境变量(CLUSTER_ENV)?
javascript ×8
reactjs ×8
html-table ×3
axios ×2
css ×2
node.js ×2
web ×2
heroku ×1
ios ×1
json ×1
material-ui ×1
nsdictionary ×1
react-native ×1
rest ×1
server ×1
setstate ×1
swift ×1
yarnpkg ×1