jquery AJAX 调用总是抛出xhr.send(选项.hascontent &&options.data空)错误

blpfk2vs  于 2023-03-17  发布在  jQuery
关注(0)|答案(2)|浏览(534)

我正在尝试 AJAX 调用。但是Post调用在“xhr.send(options.hascontent &&options.datanull)”行给出了400错误。
以下是代码,供参考:

$.ajax({
        type: "POST",
        url: "/Security/Login",
        async: false,
        contentType: false,
        beforesend: function (xhr) {
            xhr.setRequestHeader("X-XSRF-TOKEN", $('input:hidden[name =" __RequestVerificationToken"]').val());
        },
        datatype: "json",
        data: "{'Hi'}",        
        processData: false,        
        success: function (response) {

        },
        complete: function (response) {

        }
    });
hrirmatl

hrirmatl1#

$.ajax({
    type: "POST",
    url: "/Security/Login",
    async: false,
    contentType: false,
    beforesend: function (xhr) {
        xhr.setRequestHeader("X-XSRF-TOKEN", $('input:hidden[name =" __RequestVerificationToken"]').val());
    },
    datatype: "json",
    data: "{'Hi'}",        
    processData: false,        
    success: function (response) {

    },
    complete: function (response) {

    }
});

降低数据部分之后的async false。

nr7wwzry

nr7wwzry2#

我在后端代码的某个地方遇到了同样的错误,当我试图填充从 AJAX 调用中检索到的对象的列表时,这是在NET核心使用C#时发生的。
我是问:

// The returned object was:
Event event = new EventViewModel()
{
    IdEvent = null,
    FileStatusList = _operationsService.GetFileStatusList(), // LINE 2
    Comment = null,
};

return Json(event); // to frontend

去掉LINE 2为我解决了前端的问题,jQuery AJAX 调用也按预期工作。

var data = {
    id: eventId
}

$.ajax({
    url: '../File/Edit', // To Edit method at File controller
    dataType: "json",
    async: true,
    data: data,
    method: "POST",
    success: function (response) {
            // Handle your response here.
    },
    error: function (ex) {
        console.error(ex.responseText);
    },
});

相关问题