blob: 854e9d8d4e60c3828d4bc25127ffa6fda6a77464 [file] [log] [blame] [raw]
import axios from 'axios';
// 使用完整的 URL
const API_BASE_URL = 'http://192.168.128.134:3001';
// 创建一个 axios 实例,配置 CORS
const api = axios.create({
baseURL: API_BASE_URL,
withCredentials: true,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
}
});
export const fetchConfig = async () => {
console.log('Fetching config from:', API_BASE_URL);
try {
const response = await api.get('/api/config');
console.log('API response:', response.data);
return response.data;
} catch (error) {
console.error('API error details:', {
message: error.message,
response: error.response,
request: error.request,
config: error.config
});
throw new Error(
error.response?.data?.error ||
error.message ||
'网络请求失败'
);
}
};
export const updateConfig = async (hosts) => {
try {
const response = await api.post('/api/config', { hosts });
return response.data;
} catch (error) {
throw new Error(error.response?.data?.error || '网络请求失败');
}
};