[英]React import form: Ajax request into axios
So I'm trying to make this ajax form with axios, but for some reason it doesn't run.所以我试图用 axios 制作这个 ajax 表格,但由于某种原因它没有运行。 It shows me the front-end but there is nothing on the back.它向我展示了前端,但后面没有任何内容。 The commented code is the ajax request that I'm trying to do with axios and the axios that I wrote is under it.注释代码是 ajax 请求,我正在尝试处理 axios 和我写的 axios 在它下面。
class IpvImport extends React.Component {
constructor(props) {
super(props);
this.state = {
requestObject: {},
submitCompleted: false,
disabled: false,
};
}
handleSubmit(event, instance) {
var that = this;
var data = this.state.requestObject;
var dta = new FormData();
if (this.state.disabled) {
return;
}
this.setState({ disabled: true });
Object.keys(data).forEach(function (key, index) {
dta.append(key, data[key]);
});
//$.ajax({
// url: window.basePageUrl + "/ImportData",
// type: "POST",
// enctype: 'multipart/form-data',
// data: dta,
// processData: false,
// contentType: false,
// cache: false,
// timeout: 600000,
// success: function (importResult) {
// const newState = Object.assign({}, that.state);
// newState.importLog = importResult;
// that.setState(newState);
// },
// fail: function () {
// that.state.error = true;
// },
// complete: function (data) {
// that.setState({ submitCompleted: true, error: that.error, disabled: false })
// }
//});
axios.post(window.basePageUrl + "/ImportData", {
dta
})
.then(function (response) {
const newState = Object.assign({}, that.state);
newState.importLog = importResult;
that.setState(newState);
})
.catch(function (error) {
that.state.error = true;
}).then(() => {
that.setState({ submitCompleted: true, error: that.error, disabled: false })
})
event.preventDefault();
return false;
}
handleField(event, instance) {
const target = event.target;
const domType = target.tagNAME;
const inputType = target.attributes && target.attributes["type"] ?
target.attributes["type"].value : null;
var name = target.name;
var tagName = target.tagName;
if (inputType && inputType.toLowerCase() == "file") {
const newState = Object.assign({}, this.state);
newState.requestObject[name] = target.files[0];
this.setState(newState);
}
else if (tagName && tagName.toLowerCase() == "select") {
const newState = Object.assign({}, this.state);
newState.requestObject[name] = target.value;
this.setState(newState);
}
}
render() {
var that = this;
return <div class="sf-app" id="root">
<div id="root" class="sfMain sfClearfix sf-form -sf-centered-box -sf-txt-align-left">
<div class="sfContent">
<div>
<div>
<label class="sf-field__label ng-tns-c9-8 ng-star-inserted">
<h1 class="title">File import</h1><br />
<h4 class="sub_title">Click on the button and select the file that you want to upload, then click import to import it</h4>
<h4 class="summary">File to import (.xlsx):</h4><br />
</label><br />
<input type="file" onChange={event => that.handleField(event, that)} id="XlsxFile" name="XlsxFile" /><br /><br />
</div>
</div>
<div>
<button type="button" id="submittBtn" class='sf-button -action' onClick={event => that.handleSubmit(event, that)} disabled={this.state.disabled}>
<span class="sf-button__content">
{this.state.disabled ? 'Importing...' : 'Import'}
</span>
</button>
</div><br />
{this.state.importLog != null ? Logs(this.state.importLog) : null}
</div>
</div>
</div>
}
}
const Logs = (props) => {
return (
<div class="log_panell">
<strong>Import log:</strong>
<ol>
{props.map(function (p) {
return <li class={p.Severity.toLowerCase()} dangerouslySetInnerHTML={{ __html: p.Message }}></li>
})}
</ol>
</div>
)
}
ReactDOM.render(
<IpvImport />,
document.getElementById('root')
);
FYI: I'm front ender, I'm newby with the React and I'm trying to learn and I'll be gratefull if you send me some links that I can read and learn from them.仅供参考:我是前端,我是 React 的新手,我正在努力学习,如果你给我一些我可以阅读和学习的链接,我将不胜感激。 Thanks in advance提前致谢
axios.post(window.basePageUrl + "/ImportData", dta).....
dta should be request payload数据应该是请求有效载荷
You have error in this line你在这一行有错误
axios.post(window.basePageUrl + "/ImportData", {
dta
})
In ES6 {dta} shorthand to {dta: dta}
as 2nd parameter of axios expects body of the http request.在 ES6 {dta} shorthand to {dta: dta}
作为 axios 的第二个参数需要 http 请求的主体。 as you have already Form Object with you you can directly pass it like this因为你已经有表格 Object 你可以像这样直接传递
axios.post(window.basePageUrl + "/ImportData", dta)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.