坊 拓*_* 拓磨 0 google-sheets cors google-apps-script reactjs axios
CORS
当我尝试通过 Web 应用程序将数据发布到我的 Google 电子表格时收到错误响应。这是我得到的错误:
从源“http://localhost:3000”访问“myGoogleSpreadSheetApiURL”处的 XMLHttpRequest 已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:不存在“Access-Control-Allow-Origin”标头在请求的资源上。
我做了一些解决方案,我在互联网上搜索,但我无法解决问题......我已经可以从 Google 电子表格中获取我的 JSON 数据。
当我推送我的 时createButton
,我可以在我的 Google 电子表格上发布和写入我的数据。
我应该如何修复我的代码?你有什么主意吗 ?
这是我的 react.js 代码:
import React,{ Component } from 'react';
import Paper from '@material-ui/core/Paper';
import axios from 'axios';
axios.defaults.baseURL = 'http://localhost:3000';
axios.defaults.headers.post['Content-Type'] = 'application/json;charset=utf-8';
axios.defaults.headers.post['Access-Control-Allow-Origin'] = '*';
const api = 'https://myGoogleSpreadSheetApiUrl';
class Price extends Component {
state = {
info: []
};
constructor(){
super()
axios.get(api)
.then((res) =>{
console.log(res.data)
this.setState({
info: res.data
})
})
};
createInfo = () =>{
let res = axios.post(api,{
id: 100,
product: "product100",
price: 1000,
miniLot: 1000,
})
console.log(res)
}
render(){
return (
<div>
<button onClick={this.createInfo}>createButon</button>
<Paper>
{this.state.info.map(info => <p key={info.id}>{info.product}</p>)}
</Paper>
</div>
);
}
}
export default Price;
Run Code Online (Sandbox Code Playgroud)
这是我的 Google Apps 脚本代码:
function getData(priceDB) {
var sheet = SpreadsheetApp.getActive().getSheetByName(priceDB);
var rows = sheet.getDataRange().getValues();
var keys = rows.splice(0, 1)[0];
return rows.map(function(row) {
var obj = {};
row.map(function(item, index) {
obj[String(keys[index])] = String(item);
});
return obj;
});
}
function doGet() {
var data = getData('DBprice');
return ContentService.createTextOutput(JSON.stringify(data, null, 2))
.setMimeType(ContentService.MimeType.JSON);
}
function doPost(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('DBprice');
var PostData = JSON.parse(e.postData.contents);
sheet.appendRow([PostData.id, PostData.product, PostData.price]);
}
Run Code Online (Sandbox Code Playgroud)
Only the following HTTP methods are supported by Google apps script web- application currently:
POST
GET
OPTIONS
method is currently not supported. So, if requests from your React web-app is preflighted, the requests will fail(http-status-code-405). To avoid preflighting, consider changing the post body and Content-Type
to one of the following types(so called Simple requests):
Also, In order to avoid redirection to a html page, you should return
text from the server side.
Client side:
axios.defaults.headers.post['Content-Type'] = 'text/plain';
/*....*/
createInfo = () =>{
let res = axios.post(api,JSON.stringify({
id: 100,
product: "product100",
price: 1000,
miniLot: 1000,
})
)
//console.log(res)
}
Run Code Online (Sandbox Code Playgroud)
Server side:
function doPost(e) {
/*Stuff*/
return ContentService.createTextOutput("done");
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2721 次 |
最近记录: |