0

私のリアクトアプリで PUT リクエストに問題があります。標準フェッチリクエストで redux-form を使用しています。リクエストを送信するとエラーが発生しました: PUT http://localhost:8080/auth 500 (Internal Server Error) "SyntaxError: Unexpected token <" フェッチ用のコードがあります:

export default async function submit(fields, dispatch) {
    console.trace()
    const {email, fullName, company, industry, phone} = fields
    let errors
    if (!email)
        errors = {
            ...errors,
            email: 'Email cannot be empty'
        }; else if (!/[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}/i.test(email))
        errors = {...errors, email: 'Email seems invalid'};

    if (!fullName)
        errors = {...errors, fullName: 'Full name cannot be empty'};

    if (!company)
        errors = {...errors, company: 'Company name cannot be empty'};

    if (!industry)
        errors = {...errors, industry: 'Industry should be selected'};

    if (errors) {
        return Promise.reject(errors)
    }
    const response = await fetch('/auth', {
        credentials: 'same-origin',
        method: 'PUT',
        body: Object.keys(fields)
            .filter(prop => fields[prop] !== undefined)
            .reduce((fd, prop) => {
                fd.append(prop, fields[prop])
                return fd
            }, new FormData())
    });
    if (response.ok) {
        console.log('good');
        console.log(response);
    } else {
        const data = await response.json();
        console.log('bad');
        console.log(response);
    }
}

何が問題なのですか?

4

1 に答える 1

1

これは、呼び出しの前redux-formに a を置いた場合を除いて、実際には とは関係ありません。console.log(fields)fetch()

FormDataこれは、JSON を質問にシリアライズしたものです。FormDataそうは言っても、「reduce into 」コードに問題があるとは思えません。多分それを変数に保存してコンソールに出力しますか?

それか、サーバー側でその構造のデータが予期されていない問題が発生している可能性があります。通常はプレーンな JSON で送信しますが、スタックを完全に制御できます。

于 2016-04-08T18:36:15.947 に答える