成功功能的ajax响应不起作用

问题描述

我在asp.net核心应用程序中创建了一个弹出窗口,以使用ajax创建和编辑新客户,但是在发布数据时,出现此错误:

{readyState:4,getResponseHeader:ƒ,getAllResponseHeaders:ƒ, setRequestHeader:ƒ,overrideMimeType:ƒ,...}

中止:ƒ(statusText)始终:ƒ()捕获:ƒ(fn)完成:ƒ()失败: ƒ()getAllResponseHeaders:ƒ()getResponseHeader:ƒ(键) overrideMimeType:ƒ(type)管道:ƒ(/ * fnDone,fnFail,fnProgress * /)进度:ƒ()承诺:ƒ(obj)readyState:4 responseText:“ {” isValid“:true,” html“:{” setRequestHeader:ƒ(名称,值)状态: ƒ()状态:200 statusCode:ƒ(map)statusText:“ parsererror”然后: ƒ(onFulfilled,onRejected,onProgress) 原始:对象

enter image description here

这是我在控制器中的功能

[HttpGet]
    public IActionResult CreateOrUpdate(int id = 0)
    {
        if (id == 0)
            return View(new Customer());
        else
        {
            var CustomerModel = _customerRepository.Find(id);
            if (CustomerModel == null)
            {
                return NotFound();
            }
            return View(CustomerModel);
        }
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public IActionResult CreateOrUpdate(Customer customer)
    {
        if (ModelState.IsValid)
        {
            //Insert
            if (customer.CustomerId == 0)
            {
                 _customerRepository.Create(customer);

            }
            //Update
            else
            {
               
                    _customerRepository.Update(customer);
             
            }
            return Json(new { isValid = true,html = GlobalClass.RenderRazorViewToString(this,"_ViewAllCustomer",_customerRepository.GetAll()) });
        }


        return Json(new { isValid = false,"CreateOrUpdate",customer) });
    }

这是我在site.js中的ajax方法

PostCustomer = form => {
 
        $.ajax({
            type: 'POST',url: form.action,data: new FormData(form),contentType: false,processData: false,success: function (result) {
                if (result.isValid) {
                    $('#view-all-customer').html(result.html)
                    $('#form-modal .modal-body').html('');
                    $('#form-modal .modal-title').html('');
                    $('#form-modal').modal('hide');
                }
                else
                    $('#form-modal .modal-body').html(result.html);
            },error: function (err) {
                console.log(err)
            }
        })
        return false;
    
    }

这是我的视图CreateOrUpdate.cshtml

@model Library.Models.Customer

@{
    ViewData["Title"] = "Create New Customer";
    Layout = null;
}

<div class="row">
    <div class="col-lg-12">
        <form asp-controller="Customer" asp-action="CreateOrUpdate" asp-route-id="@Model.CustomerId" onsubmit="return PostCustomer(this);">

            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <input type="hidden" asp-for="@Model.CustomerId" />
                <div class="form-group">
                    <label asp-for="Name" class="control-label"></label>
                    <div class="col-lg-12">
                        <input asp-for="Name" class="form-control" />
                        <span asp-validation-for="Name" class="text-danger"></span>
                    </div>
                </div>
                <div class="form-group">
                    <div class="btn-group col-md-12">
                        <input type="submit" value="Create" class="btn btn-primary center-block" />
                    </div>
                </div>
</form>
    </div>
</div>


@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

有人可以帮我吗:(?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...