我试图从docker容器中的oracle数据库中获取SQL查询,然后在web界面中以下拉列表的形式显示响应。这是服务器端的get方法:
app.get('/api/getDropdownOptions', (req, res) => {
oracledb.getConnection(dbConfig, (err, connection) => {
if (err) {
console.error(err.message);
return;
}
console.log('Connected to Oracle Database');
const sql = 'SELECT nume FROM JUDETE';
connection.execute(sql, (err, result) => {
if (err) {
console.error(err.message);
res.status(500).json({ error: 'Failed to fetch dropdown options' });
} else {
console.log(result.rows);
const dropdownOptions = result.rows.map((row) => ({ nume: row[0] }));
console.log(typeof dropdownOptions)
console.log(dropdownOptions)
res.setHeader('Content-Type', 'application/json');
res.send(dropdownOptions);
}
});
connection.close((err) => {
if (err) {
console.error(err.message);
}
console.log('Disconnected from Oracle Database');
});
});
});
这是react页面:
import React, { useState, useContext, useEffect } from 'react';
import './DatePersonale.scss';
import { useNavigate } from 'react-router-dom';
import { faExclamationCircle } from '@fortawesome/free-solid-svg-icons';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import FormContext from './FormContext';
export default function DatePersonale() {
const navigate = useNavigate();
const { formData, updateFormData } = useContext(FormContext);
const [dropdownOptions, setDropdownOptions] = useState([]);
const [formErrors, setFormErrors] = useState({
cnp: '',
nume: '',
prenume: '',
email: '',
telefon: '',
judet: '',
adresa: '',
});
useEffect(() => {
// Fetch the dropdown options from the server
fetchDropdownOptions();
}, []);
const fetchDropdownOptions = async () => {
try {
const response = await fetch('/api/getDropdownOptions');
console.log(typeof response, response);
//console.log(await response.text())
const data = await response.json();
console.log(data);
setDropdownOptions(data);
} catch (error) {
console.error('Error fetching dropdown options:', error);
}
};
const handleInputChange = (e) => {
const { id, value } = e.target;
updateFormData(id, value);
console.log(formData);
setFormErrors((prevErrors) => ({
...prevErrors,
[id]: '',
}));
};
const inputFields = [
{ id: 'cnp', label: 'CNP' },
{ id: 'nume', label: 'Nume' },
{ id: 'prenume', label: 'Prenume' },
{ id: 'email', label: 'Adresă email' },
{ id: 'judet', label: 'Județ' },
{ id: 'telefon', label: 'Telefon' },
{ id: 'adresa', label: 'Adresă domiciliu' },
];
const handleSubmit = (e) => {
e.preventDefault();
const errors = {};
inputFields.forEach((field) => {
if (formData[field.id].trim() === '') {
errors[field.id] = 'Va rugăm completați câmpul';
}
});
setFormErrors(errors);
console.log(Object.keys(errors).length);
if (Object.keys(errors).length === 0) {
navigate('/creare-cont/date-cont');
}
};
return (
<div>
<div className="main-section-header">
<button className="deactivated">
1 <span>Date personale</span>
</button>
<button onClick={() => navigate('/creare-cont/date-cont')}>2</button>
<button onClick={() => navigate('/creare-cont/date-card')}>3</button>
</div>
<div className="main-section-content">
<form className="form-creare-cont" onSubmit={handleSubmit}>
<div className="part">
{inputFields.slice(0, 4).map((field) => (
<div className="form-element" key={field.id}>
<label htmlFor={field.id}>
<span className='label-text'>
{field.label} *
{formErrors[field.id] && (
<p className="error">
<i>
<FontAwesomeIcon icon={faExclamationCircle} />
</i>
{formErrors[field.id]}
</p>
)}
</span>
<input
type="text"
id={field.id}
value={formData[field.id]}
onChange={handleInputChange}
required
/>
</label>
</div>
))}
</div>
<div className="part">
<div className="form-element">
<label htmlFor="judet">
<span className="label-text">
Județ *
{formErrors.judet && (
<p className="error">
<i>
<FontAwesomeIcon icon={faExclamationCircle} />
</i>
{formErrors.judet}
</p>
)}
</span>
<select
id="judet"
value={formData.judet}
onChange={handleInputChange}
required
>
<option value="">Selectați județul</option>
{dropdownOptions.map((option, index) => (
<option key={index} value={option.nume}>
{option.nume}
</option>
))}
</select>
</label>
</div>
{inputFields.slice(6).map((field) => (
<div className="form-element" key={field.id}>
<label htmlFor={field.id}>
<span className='label-text'>
{field.label} *
{formErrors[field.id] && (
<p className="error">
<i>
<FontAwesomeIcon icon={faExclamationCircle} />
</i>
{formErrors[field.id]}
</p>
)}
</span>
<input
type="text"
id={field.id}
value={formData[field.id]}
onChange={handleInputChange}
required
/>
</label>
</div>
))}
</div>
</form>
<div className="bottom">
<button type="submit" onClick={handleSubmit}>
Înainte
</button>
</div>
</div>
</div>
);
}
我得到:语法错误:JSON.parse:JSON数据的第1行第1列出现意外字符当我在POSTMAN中调用该API时,它返回良好的JSON格式:
[
{
"nume": "Bucuresti"
},
{
"nume": "Alba"
},
{
"nume": "Arad"
},
{
"nume": "Arges"
},
{
"nume": "Bacau"
},
{
"nume": "Bihor"
},
{
"nume": "Bistrita-Nasaud"
},
{
"nume": "Botosani"
},
{
"nume": "Braila"
},
{
"nume": "Brasov"
},
{
"nume": "Buzau"
},
{
"nume": "Calarasi"
},
{
"nume": "Caras-Severin"
},
{
"nume": "Cluj"
},
{
"nume": "Constanta"
},
{
"nume": "Covasna"
},
{
"nume": "Dambovita"
},
{
"nume": "Dolj"
},
{
"nume": "Galati"
},
{
"nume": "Giurgiu"
},
{
"nume": "Gorj"
},
{
"nume": "Harghita"
},
{
"nume": "Hunedoara"
},
{
"nume": "Ialomita"
},
{
"nume": "Iasi"
},
{
"nume": "Ilfov"
},
{
"nume": "Maramures"
},
{
"nume": "Mehedinti"
},
{
"nume": "Mures"
},
{
"nume": "Neamt"
},
{
"nume": "Olt"
},
{
"nume": "Prahova"
},
{
"nume": "Salaj"
},
{
"nume": "Satu Mare"
},
{
"nume": "Sibiu"
},
{
"nume": "Suceava"
},
{
"nume": "Teleorman"
},
{
"nume": "Timis"
},
{
"nume": "Tulcea"
},
{
"nume": "Valcea"
},
{
"nume": "Vaslui"
},
{
"nume": "Vrancea"
}
]
我不明白为什么会这样。
.................................................
1条答案
按热度按时间hl0ma9xz1#
看起来你没有得到JSON的响应。尝试添加content-type:application/json header到您的请求。
你可以在chrome开发工具中的“网络选项卡”中验证你的请求的响应类型。你可以在Chrome dev tools上了解更多关于chrome开发工具的信息。