数据在对服务器的 POST 请求中丢失

时间:2021-05-10 19:50:01

标签: javascript jquery json

这是一个非常奇怪的错误。我有一个 redux 操作,它获取表单数据,然后将它作为 jQuery.ajax 发送到服务器。 (我知道,这不好,但我不是那样写的)

该错误仅影响一个字段 Vehicle_id,值为字符串。

    console.log('This gets sent', requestConfig) //This contains the stringified data. `vehicle_id` has the correct value.
    //It's the output of JSON.Stringify(data) with parameters like method and url added
    return new ApiRequest({
        dispatch: dispatch,
        getState: getState,
        config: requestConfig, // This is the same requestConfig, vehicle_id is null.
        done: function(data) {
            ...
        }
        ...

ApiRequest 是一个自定义类。它将 requestConfig 按原样传递给 jQuery.ajax 函数。

可能导致这种行为的原因是什么?

requestConfig(从 console.log 从控制台粘贴):

data: "{\"date\":\"2021-05-10\",\"warehouse_id\":\"1\",\"supplier_id\":\"2\",\"vehicle_id\":\"301\",\"suppliers_number\":\"\",\"note\":\"\",\"items\":[{\"stock_item_id\":\"\",\"quantity\":1,\"price\":0,\"currency_id\":\"8\"},{\"stock_item_id\":\"\",\"quantity\":1,\"price\":0,\"currency_id\":\"8\"},{\"stock_item_id\":\"\",\"quantity\":1,\"price\":0,\"currency_id\":\"8\"}]}"
method: "POST"
url: "/goods-received-notes"

ApiRequest 类(管理配置的部分)

    import appConfig from '../config'
import { refreshToken } from '../actions/auth'

export function formatApiError(messageBase, xhr) {
    if (xhr.status === 422) { // Validation error
        let message = messageBase + '.';

        Object.keys(xhr.responseJSON).map((fieldName) => {
            message += '<br>' + xhr.responseJSON[fieldName];
        });

        return message;
    }
    else { // Any other error
        return messageBase + ': ' + xhr.responseJSON.message + '.';
    }
}

export class ApiRequest {
    constructor(options) {
        this.ajaxRequest = null;

        this.config = options.config;
        this.doneCallback = options.done;
        this.failCallback = options.fail;

        this.dispatch = options.dispatch;
        this.getState = options.getState;
    }

    run() {
        const dispatch = this.dispatch;
        const failCallback = this.failCallback;
        const repeatedRequest = this;

        let config = {
            ...this.config,
            url: appConfig.api.url + this.config.url,
            contentType: 'application/json',
            accept: 'application/json',
        }

        if (this.getState().user.object) {
            const token = this.getState().user.object.token;

            if (token) {
                config.headers = {
                    ...config.headers,
                    Authorization: 'Bearer ' + token
                }
            }
        }

        this.ajaxRequest = $.ajax(config)
            .done(this.doneCallback)
            .fail(function(xhr, exception) {
                    if (xhr.status === 401 && xhr.responseJSON.message === 'Token has expired') {
                    let tokenRefreshResult = dispatch(refreshToken(repeatedRequest));
                }
                    else if (exception !== 'abort' && failCallback !== undefined) {
                    failCallback(xhr);
                }
            });
    }

    abort() {
        this.ajaxRequest.abort();
    }
}

export class DownloadRequest {
    constructor(options) {
        this.config = options.config;
        this.getState = options.getState;
        this.newWindow = options.newWindow
    }

    run() {
        const token = this.getState().user.object.token;
        const url = appConfig.api.url + this.config.url + '?token=' + token;

        if (this.newWindow) {
            window.open(url);
        }
        else {
            window.location.assign(url);
        }
    }
}

export function ajax({ dispatch, getState, config, done, error } = {}) {
    let ajaxRequest = (repeatedRequest) => {
        $.ajax(config)
        .done(done)
        .fail(function(xhr) {
                if (xhr.status === 401 && xhr.responseJSON.message === 'Token has expired') {
                let tokenRefreshResult = dispatch(refreshToken(
                    repeatedRequest
                ));
            }
            else {
                error(xhr);
            }
        });
    };

    ajaxRequest(ajaxRequest);
}

0 个答案:

没有答案
相关问题