gpt4 book ai didi

javascript - POST 请求适用于 Postman,但不适用于 axios 或 .fetch()

转载 作者:可可西里 更新时间:2023-11-01 13:27:23 28 4
gpt4 key购买 nike

我遇到了一个问题,我已经处理了几天,但找不到解决方案。我用 Lumen 创建了一个 API,用 ReactJS 创建了一个前端。这一切都适用于 GET 请求,但是当我发送 POST 请求时它失败了。由于一些奇怪的原因,当我用 Postman 发送请求时,这些请求有效。现在是一些代码!

首先是发送请求的 JS 脚本:

import moment from 'moment';
import React, {Component} from 'react';
import { Modal, Form, Button, Input, DatePicker, Select, message } from 'antd';

const {RangePicker} = DatePicker;
const FormItem = Form.Item;
const Option = Select.Option;

const api_url = 'api/v1/';

class NewEventForm extends React.Component {
constructor(props) {
super(props);

this.state = {
confirmLoading: false,
categories: []
};

this.onCreate = this.onCreate.bind(this);
this.onCancel = this.onCancel.bind(this);
}

componentDidMount() {
fetch(api_url + 'category')
.then(results => {
return results.json();
}).then(data => {
let categories = data.map((cat) => {
return (
<Option key={cat.id} value={cat.id}>{cat.name}</Option>
);
});
this.setState({categories: categories});
});
}

updateStates() {
this.props.updateState();
this.setState({confirmLoading: false});
}

onCreate() {
this.props.form.validateFields((err, values) => {
this.setState({
confirmLoading: true
});

if (err) {
this.setState({
confirmLoading: false
});
return;
}

let event = {
title: values.title,
description: values.description,
start_time: values.date[0],
end_time: values.date[1],
category_id: values.category
};

fetch(api_url + 'event', {
method: 'POST',
/* headers are important*/
headers: {
"content-type":"application/json",
"cache-control":"no-cache",
"accept":"*/*",
},
body: JSON.stringify(event)
}).then(response => {
if(response.ok) {
return response.json();
}
throw new Error("Antwort der API war nicht 'Ok'!");
}).then(data =>{
this.updateStates();

message.success('Das Ereignis wurde erfolgreich eingetragen!');
}).catch(error => {
//console.log(error);
this.updateStates();

message.error('Das Ereignis wurde nicht erstellt. Bitte versuche es später nochmal!');
});
});
}
onCancel(e) {
e.preventDefault();

this.updateStates();
}

render() {
const {getFieldDecorator, getFieldError} = this.props.form;

return(
<Modal title="Neue Veranstaltung hinzufügen" okText="Eintragen" confirmLoading={this.state.confirmLoading} visible={this.props.visible} onOk={this.onCreate} onCancel={this.onCancel}>
<Form layout="vertical">
<FormItem label="Titel">
{getFieldDecorator('title', {
rules: [{required: true, message: 'Bitte einen Titel angeben!' }],
})(
<Input />
)}
</FormItem>
<FormItem label="Beschreibung">
{getFieldDecorator('description')(<Input type="textarea" />)}
</FormItem>
<FormItem label="Kategorie">
{getFieldDecorator('category', {
rules: [{required: true, message: 'Bitte einen Kategorie auswählen!' }],
})(
<Select placeholder="Kategorie auswählen...">
{this.state.categories}
</Select>
)}
</FormItem>
<FormItem label="Zeitraum" className="collection-create-formlast-form-item">
{getFieldDecorator('date', {
rules: [{required: true, message: 'Bitte einen Zeitraum auswählen!' }],
})(
<RangePicker
showTime={{
hideDisabledOptions: true,
defaultValue: [moment('00:00', 'HH:mm'), moment('00:00', 'HH:mm')],
format: 'HH:mm'
}}
format="DD.MM.YYYY HH:mm"
/>
)}
</FormItem>
</Form>
</Modal>
);
}
}

export const NewEventModal = Form.create()(NewEventForm);

我的数据库有三个模型。事件、类别和用户:Category.id<---1:n--->Event.category_id||Event.updated_by<---n:1--->User.id

现在是事件 Controller :

<?php

namespace App\Http\Controllers;

use App\Event;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;


class EventController extends Controller{


public function index(){
$Events = Event::all();

return response()->json($Events);
}

public function getEvent($id){
$Event = Event::with(['category', 'user'])->find($id);

return response()->json($Event);
}

public function createEvent(Request $request){
$User = \App\User::find(1);
$Category = \App\Category::find(1);

$Event = new Event;
$Event->fill($request->all());
$Event->user()->associate($User);
$Event->category()->associate($Category);

$obj = '';
foreach ($request->all() as $key => $value) {
$obj .= '[' . $key . '] => "' . $value . '"; ';
}

\Log::warning('Test: ' . $obj);

$Event->save();

return response()->json($request);
}

public function deleteEvent($id){
$Event = Event::find($id);
$Event->delete();

return response()->json('deleted');
}

public function updateEvent(Request $request,$id){
$Event = Event::find($id);
$Event->title = $request->input('title');
$Event->description = $request->input('description');
$Event->start_time = $request->input('start_time');
$Event->end_time = $request->input('end_time');
$Event->save();

return response()->json($Event);
}

}

和事件模型:

<?php
namespace App;

use Illuminate\Database\Eloquent\Model;

class Event extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['title', 'description', 'start_time', 'end_time'];

public function user() {
return $this->belongsTo('App\User', 'updated_by');
}

public function category() {
return $this->belongsTo('App\Category');
}
}

正如我所说,当我使用 Postman 发送 POST 请求时,一切都按预期工作。但是使用我的 fetch() 函数我得到了 200 响应,但是在数据库中只有“created_at”和“updated_at”被填充,其余的只是一个空字符串。 EventController 中的 Log-statement 显示,这似乎是 Request-object 为空。但是查看 Firefox 开发人员工具,我发现数据是在请求正文中发送的。

有什么想法吗?如果需要,我还可以发送其他代码文件。

谢谢大家的帮助马可

编辑: 由于不明显,API 和前端都在同一台主机上运行; localhost:8000 所以这不是 CORS 问题。我首先在 localhost:8080 上运行前端,但我通过在同一台服务器上运行两者来消除它。

最佳答案

果然在没有人真正能够直接回答我的问题的情况下,我的错并不明显。我今天和一个 friend 一起认识到,我实际发送的请求与我在代码中写的不同。通过更多搜索,我发现我的 webpack.config 配置有误,并将代码发布到错误的目录中。但是由于已经有一个“较旧”的 js 文件,该页面看起来是正确的,但没有我的 API 调用的更改。

TL;DR 注意一切都在你需要的地方然后上面的代码是正确的:-)

关于javascript - POST 请求适用于 Postman,但不适用于 axios 或 .fetch(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48240949/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com