首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用react.js和laravel无法更新带有formData的PUT请求

使用react.js和laravel无法更新带有formData的PUT请求
EN

Stack Overflow用户
提问于 2019-11-01 19:00:26
回答 2查看 449关注 0票数 0

我没有在Api端点中接收到通过fetch和PUT方法发送的表单数据。

我使用POST方法,它解决了我认为不推荐更新的问题。我在localhost:3000上运行react,在localhost:5000上运行laravel-API。

这是API中的路由

代码语言:javascript
复制
Route::put('updateSlide/{id}', 'SlidesController@updateSlide');

这就是控制器中的内容

代码语言:javascript
复制
public function updateImage(Request $request, int $id)

{

    $image = $this->slideRepo->findSlideById($id)->image;


    if ($image) {
        $result = Storage::disk('ucmp')->delete($image);
    }

    if ($request->hasFile('image') && $request->file('image') instanceof UploadedFile) {
        return  $this->slideRepo->saveCover($request->file('image'));

    }        // return response()->json($data);

    // data is an array (note)
    return null;
}


public function updateSlide(Request $request, int $id)
{
    $imageUrl=$this->updateImage($request, $id);

    return response()->json($this->slideRepo->updateSlide([
        'caption' => $request['caption'],
        'image' => $imageUrl,
        'url' => $request['url']
    ],$id));
}

这是发送到fetch的函数

代码语言:javascript
复制
export const updateSlideApi = (token, _slide, id) => {
  return {
    url: `${BASE_URL}/api/updateSlide/${id}`,
    opt: API.requestOptions("PUT",token,null,{ body: _slide }, true)
  };
};

在我的头中,我没有content-type。

我期望来自laravel API函数的json数据,但我有一个错误,“只能抛出对象”。

EN

回答 2

Stack Overflow用户

发布于 2019-11-01 19:33:50

PHP不解析PUT请求的正文。

使用POST并添加值为PUT的参数_method

票数 0
EN

Stack Overflow用户

发布于 2020-09-17 22:10:51

PHP put给了我困难的时间,使用这个函数将节省你解析它的时间,否则方法:

代码语言:javascript
复制
function parsePutRequest()
    {
        // Fetch content and determine boundary
        $raw_data = file_get_contents('php://input');
        $boundary = substr($raw_data, 0, strpos($raw_data, "\r\n"));

    // Fetch each part
    $parts = array_slice(explode($boundary, $raw_data), 1);
    $data = array();

    foreach ($parts as $part) {
        // If this is the last part, break
        if ($part == "--\r\n") break; 

        // Separate content from headers
        $part = ltrim($part, "\r\n");
        list($raw_headers, $body) = explode("\r\n\r\n", $part, 2);

        // Parse the headers list
        $raw_headers = explode("\r\n", $raw_headers);
        $headers = array();
        foreach ($raw_headers as $header) {
            list($name, $value) = explode(':', $header);
            $headers[strtolower($name)] = ltrim($value, ' '); 
        } 

        // Parse the Content-Disposition to get the field name, etc.
        if (isset($headers['content-disposition'])) {
            $filename = null;
            preg_match(
                '/^(.+); *name="([^"]+)"(; *filename="([^"]+)")?/', 
                $headers['content-disposition'], 
                $matches
            );
            list(, $type, $name) = $matches;
            isset($matches[4]) and $filename = $matches[4]; 

            // handle your fields here
            switch ($name) {
                // this is a file upload
                case 'userfile':
                    file_put_contents($filename, $body);
                    break;

                // default for all other files is to populate $data
                default: 
                    $data[$name] = substr($body, 0, strlen($body) - 2);
                    break;
            } 
        }

    }
    return $data;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58658091

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档